Character returns to idle animation when moving left or any different directions instead of facing the direction in where it moved
Hello,
I'm taking a game development class and my professor wants myself and my group members to make a game so I am quite to unity. The problem I'm having is that when my player moves let's say left, the running animation works, but the character goes back to the idle animation instead of rotating to face the left side. How can I fix this error? My colleagues and I are working on a 2D top down shooting game. Here's the code that we have so far. Can someone please help, thank you.
Unity 2D Character goes back to idle animation instead of facing the direction in which the character is running towards
public class BasicMovement : MonoBehaviour
{
// Start is called before the first frame update
public float speed;
public float horizontalMove = 0f;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
public Animator animator;
void Update()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal")*speed, Input.GetAxis("Vertical") * speed, 0.0f);
rb.AddForce(movement * speed);
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
transform.position = transform.position + movement * Time.deltaTime;
}
}
You should consider handling the transitions between animations with the Animator Controller: https://docs.unity3d.com/$$anonymous$$anual/class-AnimatorController.html It's a good tool to save you lots of work.
Answer by ejoo_pasco · Sep 19, 2019 at 08:27 PM
The idle animation per se is not wrong, is it? The rotation is. So you have to delete the keyframes from the idle animation clip, that rotate the player back. Then the character does not get rotated when entering the idle animation. If you don't want the character turn to idle, check the transitions in the animator controller - where does it turn after running? It should be in SOME animation state of course. Delete all "diamonds" in the green area as told in the comment below.
So it goes from the idle blend tree to the running state and then it goes back from running state to the idle state. The idle state has a blend tree with 7 animations for idle and then running state has a blend tree with 7 animations. The running animation works fine it’s just when the person let’s say moves anywhere it wont face towards that direction ins$$anonymous$$d it faces towards the idle position which is facing forward
Delete all "diamonds" in the green area in all idle animation clips
Answer by chrismoran95 · Sep 19, 2019 at 03:07 PM
I already have a blend tree created for my idle animations and running animations. It still returns to the idle animation instead of facing to where ever the character is looking at