- Home /
Falling down the Y-axis?
Hello,
At the moment i'm refining my character movement script (and my very first script ever) which uses a character controller. The question I have is, is there perhaps a way to detect if the character is moving down a certain axis?
I ask because I have the jump set up just fine, but I would like my character to switch into a falling animation if he starts moving down the Y axis until the character is grounded. I've looked around at other scripts with a fall applied but they don't seem to be what i'm looking for.
I'm using moveDirection.y for the jump translations. Here's a snippet of the jump part of my code (minus the animation section):
if (Input.GetButton("Jump"))
{ moveDirection.y = jumpSpeed; } } //Apply gravity moveDirection.y -= gravity * Time.deltaTime;
Or is there maybe a better way i'm not thinking of to detect a fall?
Answer by DaveA · Jul 20, 2012 at 05:10 PM
You could save the position from frame to frame, subtract current from last (divide by delta time if you like) and see what the Y component looks like. Or use rigidbody's velocity component. You want to see that y is in the negative direction (positive would be a jump)
Thank you for mentioning the rigidbody's velocity component. I was going insane trying to get a falling setup, but the gravity subtracting from y was making it almost impossible to go by that route. Hours of fiddling turned into:
if (this.playerController.velocity.y < 0)
Answer by Meater6 · Jul 20, 2012 at 05:15 PM
I would think that the jump code would be irrelevant, because for example, the character just walks of a cliff without jumping, it would not play the falling animation. I would just use the character controller's collision flags or the isGrounded function to see if the controller is in the air. I would also check to see if the y component of the velocity is negative. If, as in some cases, your character should be playing the fall animation, but is not moving down, I would make a variable to store whether the character is "falling" (a.k.a. in the air & not jumping). If this is true, play the falling animation.
If this is your first script ever, it sounds very impressive. In case you don't know the scripting reference, here it is.
Good luck.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074453im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Another Slow Fall Problem... 2 Answers
the ball falls in a straight line sloping 0 Answers
Jumping script WONT WORK!!!!!!!! for 2D 1 Answer
My character does not go forward when I jump? 0 Answers
Falling Damage 1 Answer