- Home /
Fall animation when character is falling
As far as my controller script is set up. When I press the jump key, the "Jump" animation plays. However, the "FallDown" animation plays immediately after and keeps playing until my character is grounded. I tried some adjustment with my scripts but had no luck.
I want my "Jump" animation to play then clamp on the last frame until I my y velocity is <= 0.
I want my "FallDown" animation to play when I start falling (y velocity < 0).
I'll add a "Land" animation latter, so I don't want the "FallDown" animation playing when my character is grounded.
Please feel free to add suggestions/solutions.
Here's the parts of my scripts that involve the jumping:
//In air animations
else
{
if(Input.GetButtonDown("Jump"))
{
animation.CrossFade("Jump");
animation["Jump"].speed = 3;
}
if(!playerController.IsGrounded())
{
animation.CrossFade("FallDown", 0.5);
animation["FallDown"].speed = 3;
}
}
(Controller)
//Private, helper variables
private var moveDirection = Vector3.zero;
private var grounded : boolean = true;
private var moveHorz = 0.0;
private var normalHeight = 2.0;
private var crouchHeight = 1.7;
private var rotateDirection = Vector3.zero;
private var isCrouching : boolean = false;
private var isRunning : boolean = false;
//Cache controller so we only have to find it once
var controller : CharacterController;
controller = GetComponent(CharacterController);
var MoaliaStatus : Moalia_Status;
MoaliaStatus = GetComponent(Moalia_Status);
//Move the controller during the fixed frame updates
function FixedUpdate()
{
if(!isControllable)
Input.ResetInputAxes();
else
{
if(grounded)
{
//Since we're touching something solid, such as the ground, allow movement
//Calculate movement directly from input axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= walkSpeed;
moveDirection.y -= gravity * Time.deltaTime;
//Find rotation based upon axes if need to turn
moveHorz = Input.GetAxis("Horizontal");
if (moveHorz > 0) //Right turn
rotateDirection = new Vector3(0, 1, 0);
else if (moveHorz < 0) //Left turn
rotateDirection = new Vector3(0, -1, 0);
else //not turning
rotateDirection = new Vector3(0, 0, 0);
//Jump controls
if(Input.GetButton("Jump"))
{
inair = true;
if(Input.GetButton("Run") && Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed/2;
}
else
{
moveDirection.y = jumpSpeed;
}
}
//Aplly any boosted speed
if(Input.GetButton("Run") && Input.GetAxis("Vertical"))
{
if(MoaliaStatus.stamina > 0)
{
isRunning = true;
if(MoaliaStatus)
{
moveDirection *= runSpeed;
MoaliaStatus.stamina -= MoaliaStatus.runUsage * Time.deltaTime;
isRunning = true;
}
}
else if(MoaliaStatus.stamina <= 0)
{
isRunning = false;
}
}
if(MoaliaStatus)
{
if(MoaliaStatus.stamina <= MoaliaStatus.maxStamina)
{
MoaliaStatus.stamina += MoaliaStatus.staminaRecharge * Time.deltaTime;
}
}
}
function IsGrounded()
{
return grounded;
}
Updated Animation Script:
if(!playerController.IsGrounded() && playerController.moveDirection.y < 0)
{
animation.CrossFade("FallDown", 0.5);
animation["FallDown"].speed = 3;
}
I also had to change the moveDirection variable as public.
Answer by LessThanEpic · Aug 23, 2014 at 03:49 AM
Add an extra condition to your if statement for the falling animation. Something like:
if(!playerController.IsGrounded() && playerController.velocity.y < 0)
{
animation.CrossFade("FallDown", 0.5);
animation["FallDown"].speed = 3;
}
That got me this error:
"NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.GetProperty (System.Object target, System.String name) UnityScript.Lang.UnityRuntimeServices.GetProperty (System.Object target, System.String name) $$anonymous$$oalia_Animation.Update () (at Assets/Scripts/Character/$$anonymous$$oalia_Animation.js:83)"
Note: In case you changes involve me using a rigid body. I'm only using a character controller.
Answer by Andres-Fernandez · Aug 23, 2014 at 03:37 PM
Check this tutorial. The part with the blend tree might be helpful.
Answer by Cherno · Aug 23, 2014 at 06:11 PM
Have a look at this tutorial:
Creating a controllable character
It determines the zenit of the jump and thus you can easily play a falling animation after the character has jumped up in the air and is coming down again.
I'll have to rewrite the entire script (or any additional scripts). If I set the Wrap$$anonymous$$ode to clamp, how can I make it go back to idle or any movement animation when it is grounded?
This is all detailed in the tutorial. The idle animation plays continously in the background but gets overweighted by running and jumping if certain criteria are met.