Можно как-то просто определить в этом же скрипте что вот управление для первого, а вот для второго персонажа? чтобы не плодить армию скриптов..
Есть такой скрипт управления
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);
}
}