Hi I’ve tried everything but my player doesn’t move but the animations play (i mean that when i press the left arrow the left walking animation plays and the same for the other directions. The attack animation also plays when i press space). Please help
This is my code:
public enum PlayerState{
walk,
attack,
idle
}
public class PlayerMovement : MonoBehaviour {
public PlayerState currentState;
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
// Use this for initialization
void Start () {
currentState = PlayerState.walk;
animator = GetComponent();
myRigidbody = GetComponent();
animator.SetFloat(“moveX”, 0);
animator.SetFloat(“moveY”, -1);
}
// Update is called once per frame
void Update () {
change = Vector3.zero;
change.x = Input.GetAxisRaw(“Horizontal”);
change.y = Input.GetAxisRaw(“Vertical”);
if(Input.GetButtonDown(“attack”) && currentState != PlayerState.attack)
{
StartCoroutine(AttackCo());
}
else if (currentState == PlayerState.walk || currentState == PlayerState.idle)
{
UpdateAnimationAndMove();
}
}
private IEnumerator AttackCo()
{
animator.SetBool(“attacking”, true);
currentState = PlayerState.attack;
yield return null;
animator.SetBool(“attacking”, false);
yield return new WaitForSeconds(.3f);
currentState = PlayerState.walk;
}
void UpdateAnimationAndMove()
{
if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat(“moveX”, change.x);
animator.SetFloat(“moveY”, change.y);
animator.SetBool(“moving”, true);
}
else
{
animator.SetBool(“moving”, false);
}
}
void MoveCharacter()
{
change.Normalize();
myRigidbody.MovePosition(
transform.position + change * speed * Time.deltaTime
);
}
}