Hi! I’m making a top down game and I’m having trouble setting animations for different Idle positions.
The Idle animation depends on where my character is facing, this is the code I’ve tried so far, but it resets to the first animation instead of actually where it is facing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move2D : MonoBehaviour
{
public Animator animator;
public Rigidbody2D rb;
Vector2 movement;
public float moveSpeed = 0.05f;
public float objectUsed;
public float moveDirection;
void Start()
{
//Get the animator
animator = this.gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update(){
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
if (Input.GetKeyDown ("up")) {
animator.SetFloat("moveDirection", 0);
}if (Input.GetKeyDown ("right")) {
animator.SetFloat("moveDirection", 1);
}if (Input.GetKeyDown ("down")) {
animator.SetFloat("moveDirection", 2);
}if (Input.GetKeyDown ("left")) {
animator.SetFloat("moveDirection", 3);
} else{
}
Debug.Log(moveDirection);
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
I’ll also paste my blend tree with the conditions to check for direction and select the animation.