2D Animation Controller woes
Hi everyone
I'm trying to make my very first 2D platformer, but I've run into a few problems with the animation aspects.. I have the Animator set up with a trio of animations which run in a loop; Idle --> Jump --> Fall --> Idle. I've checked the Parameters in Play mode and they work a treat. I've got my Animator declared as a Public Animator variable and plugged in the correct Animator on the Inspector. I've also declared Animator, called Anim, with the GetComponent<> function and declared the Parameter-Variables' values in Start.
To be clear, I have a VerticalMovement float and an onGround boolean. The player starts on the ground so VerticalMovement is 0.0f, and onGround is True. The Parameters for each change are;
Idle --> Jump (VerticalMovement Greater then 0.1)
Jump --> Fall (onGround = false)
Fall --> Idle (onGround = true, VerticalMovement Less then 0.1)
Here's the code for the Jump Input, which is inside an Update;
if (Input.GetButton ("Fire1") && Time.time > nextjump) {
nextjump = Time.time + jumprate;
rb2d.AddForce (new Vector3 (0.0f, jumpHeight, 0.0f) * speed);
verticalMovement = 1.0f;
onGround = false;
}
So to be clear I'm trying to change the parameters for the Animator to change to the relevant animation. Do I need to use the Anim.Play() thing or is that irrelevant? (BTW I tried adding Aim.Play("Jump") whilst writing this underneath onGround = false, which plays Jump, goes straight to Fall and then loops Fall endlessly.)
I appreciate this is long-winded but if anyone can help I would be really appreciative!
I can take screenshots and post more if it makes it easier.
One final comment. It feels like my code isn't actually talking to the Animator at all.
It's declared in the PublicClass and the vertical$$anonymous$$ovement float and OnGround boolean are both declared at the same time, and their initial values are set in the Start function. And whilst I'm confident the variables are being changed, it feels like it's not passing any of this info back to the Animator itself...
And I'm talking in circles. Sorry.
Answer by Injourn · Feb 29, 2016 at 09:10 PM
I ran into a similar problem. What i did catch if the player was grounded by using a Raycast(2D) and used an if statement that asked if the raycast was colliding with another object directly below it. also For changing states from Jumping(In air) to falling, It may better to use velocity there rather than movment. If you are using a rigidbody, this becomes easy. In short boolean for ground check and float for air state.
Is it possible to use IF statements to deter$$anonymous$$e if the other.GaneObject is an enemy vs ground?
Your answer
![](https://koobas.hobune.stream/wayback/20220612070101im_/https://answers.unity.com/themes/thub/images/avi.jpg)