Problem is not reproducible or outdated
Animation with different behaviors for different directions.
When I press down, the animation correctly changes to go down, when I press down together with left or right, the character moves to the respective diagonal and the animation continues to go down, which is the result that I want. But ... When I'm pressing left and down for example, it moves on the respective diagonal, but the animation changes to go down instead of keeping the animation to the left, when I'm pressing left and up, the animation doesn't change, which is a very strange behavior.
I know what is causing this problem, when I change the order in which I am assigning the parameters in the animator, the behavior is the opposite. but I'm not being able to solve it.
here's a video I made to you guys see what's going on. https://youtu.be/AKM15GrSDtw
And here's my movement script.
//Components
private Rigidbody2D rb2d;
private Animator anim;
//Movement
private Vector2 movement;
private Vector2 movementVector;
//private Vector3 directionVector;
private Vector2 facing;
private bool CanMove;
//Speed Modifier
public float speed;
// Use this for initialization
void Start ()
{
//getting the components
rb2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
CanMove = true;
}
// Update is called once per frame
void Update ()
{
//getting the axis
movementVector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
//directionVector = Camera.main.ScreenToViewportPoint(Input.mousePosition);
//seeing if the the character is moving
var isMoving = ((Mathf.Abs(movementVector.x) + Mathf.Abs(movementVector.y)) > 0);
if (isMoving)
{
if(!CanMove)
return;
//calculating the movement
movement = (movementVector * speed);
//setting the isMoving to true
anim.SetBool("isMoving", true);
//calculating the facing direction
facing = rb2d.velocity.normalized;
//anim.SetFloat("moveX", directionVector.x);
//anim.SetFloat("moveY", directionVector.y);
//setting the movement variable with the movementVector
anim.SetFloat("moveX", movementVector.x);
anim.SetFloat("moveY", movementVector.y);
}
else
{
//if isn't moving, assign the facing parameter
anim.SetFloat("facingX", facing.x);
anim.SetFloat("facingY", facing.y);
//stop the movement
movement = Vector2.zero;
//set the isMoving to False
anim.SetBool("isMoving", false);
}
}
void FixedUpdate()
{
//apply the movement
rb2d.velocity = movement;
}
Answer by Harinezumi · Feb 09, 2018 at 12:14 PM
I'm not sure what is the solution, so probably the blend state settings have some issue, for example it does not allow blending the variables, or it activates a wrong state (maybe both movement left and right activate, but in reverse proportions).
The code looks fine, and you did mention that changing the order reverses the behaviour.
One last idea: what happens if you put anim.SetBool("isMoving", true);
after you assign the animation parameters?
this code is newer, it does not reverse the behavior if the order is changed as I said. assigning the variable after assigning the movements had no effect, I think a solution would be if it were possible for the blend tree to execute only one animation at a time, overriding or not, but I'm not finding an easy way to do this.
Follow this Question
Related Questions
Falling blocks skip animation while upside down 1 Answer
how to make an animation to play when a int has a certain value, and pressing space key? 0 Answers
Is there any way to trigger a popUp UI when a player steps in a specific set of tiles? 0 Answers
Orbit position controlled by mouse 2D (C#) 0 Answers
Animation location problem! 0 Answers