Hi, I’m new to unity and just wanted to ask if this is a problem with my script. This part of the code is used for making the player do the correct idle animation, but it just goes back to the front facing idle animation and I do not understand why.
if (movement.x == 1|| movement.x == -1 || movement.y == 1 || movement.y == -1) ;
{
animator.SetFloat("lastMoveX", movement.x);
animator.SetFloat("lastMoveY", movement.y);
}
Full Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public Rigidbody2D rd;
Vector2 movement;
public Animator animator;
public SpriteRenderer sr;
public void Awake()
{
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
//input
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
movement = movement.normalized;
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("speed", movement.sqrMagnitude);
if (Input.GetKey("left"))
{
if (sr != null)
{
// flip the sprite
sr.flipX = true;
}
}
if (Input.GetKey("right"))
{
// flip the sprite
sr.flipX = false;
}
if (movement.x == 1|| movement.x == -1 || movement.y == 1 || movement.y == -1) ;
{
animator.SetFloat("lastMoveX", movement.x);
animator.SetFloat("lastMoveY", movement.y);
}
}
void FixedUpdate()
{
//movement
rd.MovePosition(rd.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}