1

Можно как-то просто определить в этом же скрипте что вот управление для первого, а вот для второго персонажа? чтобы не плодить армию скриптов..

Есть такой скрипт управления

using System.Collections.Generic;
using System.Collections;
[RequireComponent(typeof(Rigidbody2D))]

public class UnitMovement : MonoBehaviour
{
    [SerializeField] private float _moveSpeed = 6;
    [SerializeField] private float _jumpForce = 7;
    private Rigidbody2D _body;
    [SerializeField] private GroundCheck _ground;

    public float MoveInput { get; set; }
    public bool OnGround => _ground.OnGround;

    private void Start ()
    {
        _body = GetComponent<Rigidbody2D>();      
    }
    
    private void FixedUpdate ()
    {
        _body.velocity = new Vector2(_moveSpeed*MoveInput, _body.velocity.y);
    }
   
    public void TryJump ()
    {
        if (_ground.OnGround)
            _body.velocity = new Vector2(0, _jumpForce);
    }
}
using System.Collections.Generic;
using System.Collections;
[RequireComponent(typeof(UnitMovement))]

public class PlayerInput : MonoBehaviour
{
    [SerializeField] private string _moveAxis = "Horizontal";
    [SerializeField] private KeyCode _jumpKey = KeyCode.UpArrow;
    private UnitMovement _target;
 
    private void Start ()
    {
        _target = GetComponent<UnitMovement>();
    }
     
    private void Update ()
    {
        _target.MoveInput = Input.GetAxis(_moveAxis);
        if (Input.GetKeyDown(_jumpKey))
        _target.TryJump();
    }
}
using System.Collections.Generic;
using System.Collections;
[RequireComponent(typeof(UnitMovement))]

public class UnitSkin : MonoBehaviour 
{
    private Animator _animator;
    private UnitMovement _unit;

    private void Awake()
        {
           _animator = GetComponent<Animator>();
        }

    void FixedUpdate()
    {
       bool isJump = Input.GetKey(KeyCode.UpArrow);
    _animator.SetBool("isJump", isJump);

    bool isWalk = Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow);
    _animator.SetBool("isWalk", isWalk);
    }
}

1 ответ 1

0

Нашел хорошую реализацию в игре nidhogg2

if (gameObject.tag == "Player1") {//player1 axis control
            whoami = false;
            otherpc = GameObject.Find ("Player2").GetComponent<PlayerController> ();
        } else {//player2 axis control
            whoami = true;
            otherpc = GameObject.Find ("Player1").GetComponent<PlayerController> ();
if (!isanimated) {
            if (!whoami) {//player1 control axis
                if (Input.GetAxisRaw ("p1Horizontal") == -1 && CheckAnimState (1, 1, 1, 0, 0, 1, 0, 0, 0)) {
                    anim.SetBool ("Run", true);
                    rb.velocity = new Vector2 (-movespeed * mytimescale, rb.velocity.y * mytimescale);
                } else if (Input.GetAxisRaw ("p1Horizontal") == 1 && CheckAnimState (1, 1, 1, 0, 0, 1, 0, 0, 0)) {
                    anim.SetBool ("Run", true);

https://github.com/ekawijayasusilo/artofduel/blob/master/Assets/Scripts/PlayerController.cs

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими пользовательским соглашением, политикой конфиденциальности и политикой о куки

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.