Adding Animations to movement
Asked Answered
S

3

0

Hi, I am trying to learn how to use unity and program along the way and I can’t find any videos or tutorials on how to add animations to your movements for a 2D character. There are plenty on how to animate 2D characters but they all stop before explaining how you incorporate them into you movement.
For example, I have an idle animation but it plays constantly even if I move the character around. How do I ensure it only plays when player is idle. How do I add a directional walking movement to the right/left , forward/back depending on whether you press w s a or d? I assume it is something I have to add to my movement script but I am just starting out and don’t know how to code for this.

Sapp answered 23/9, 2023 at 8:19 Comment(0)
M
0

Ok here is some code that I use for my game

Code 1:

Move = Input.GetAxis("Horizontal");
characterRigidBody.velocity = new Vector2(speed * Move, characterRigidBody.velocity.y);

Code 2:

if (Move < -0.0001)
{
    animator.SetBool("FacingLeft", true);
    animator.SetBool("FacingRight", false);
}
if (Move > 0.0001)
{
    animator.SetBool("FacingLeft", false);
    animator.SetBool("FacingRight", true);
}
if (Move == 0)
{
    animator.SetBool("FacingRight", false);
    animator.SetBool("FacingLeft", false);
}

And now the Animator.

Ok. Now this will be a handful.

(Sorry gotta finish this tomorow)

Measured answered 29/9, 2023 at 5:47 Comment(0)
M
0

Ok. Now you have to open the “Animator” tab. And create 2 parameters(Booleans). One is called “FacingRight”, and one is called “FacingLeft”. (Case sensitive)

Now here is the handful part. If you already have your 4 animations made in the “Animation” Tab, (You should have an Idle right, Idle left, Walking/Running Right, and Walking/Running left.) Now, do you see all those arrows? Add them by Right-clicking an animation and clicking “Make Transition.” Now here is what going to be confusing.
When you make the arrows and you click on them, there should be a spot on the right that says conditions. Here is what you add.
The arrow that goes from:
A-> C: Facing Right = true & Facing Left = false
C-> A: Facing Right = false & Facing Left = false
B-> D: Facing Right = false & Facing Left = true
D-> B: Facing Right = false & Facing Left = false
B->C: Facing Right = true & Facing Left = false
A->D:Facing Right = false & Facing Left = true

Measured answered 30/9, 2023 at 13:45 Comment(0)
S
0

Thanks so much I’ll give this a go!

Sapp answered 30/9, 2023 at 21:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.