- Home /
Using animation curves for fall and roll-over in and endless runner 3d
Hi, I'm experimenting my game which is an endless runner 3d and I came across this issue. Firstly I went through this great tutorial that taught me how to use animation curves. I applied them. Seems to me even though I add them according to the tutorial, it does't work. The curves values seem to change in the animator window parameters but the character controller doesn't seem to get those values accordingly.
I do made some changes according to my preferences that instead of using animation trigger collider box, I did an algorithm when the player is not on ground, go to fall and when touches the ground roll.
I have two curves in each animation clip such as colliderHeight
and colliderY
and I use a Character controller. ( the tutorial use Capsule Collider)
Here's my code:
private Animator anim;
private AnimatorStateInfo currentBaseState;
private static int fallState = Animator.StringToHash("Base Layer.Fall");
private static int rollState = Animator.StringToHash("Base Layer.Roll");
private static int locoState = Animator.StringToHash("Base Layer.Run");
void Update()
{
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
if (controller.isGrounded)
{
if (isGround())
{
grounded = true;
jump = false;
fall = false;
verticalVelocity = -0.5f;
if (Input.GetButtonDown("Jump") && grounded)
{
verticalVelocity = jumpForce;
grounded = false;
jump = true;
}
else if (currentBaseState.fullPathHash == fallState && !grounded)
{
Debug.Log("falling");
controller.height = anim.GetFloat("ColliderHeight");
controller.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
if (currentBaseState.fullPathHash == rollState)
{
//if (!anim.IsInTransition(0))
controller.height = anim.GetFloat("ColliderHeight");
controller.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
}
}
//else if (currentBaseState.fullPathHash == fallState)
//{
// Debug.Log("falling");
// //grounded = false;
// controller.height = anim.GetFloat("ColliderHeight");
// controller.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
//}
//else if (currentBaseState.fullPathHash == rollState)
//{
// //grounded = true;
// if (!anim.IsInTransition(0))
// controller.height = anim.GetFloat("ColliderHeight");
// controller.center = new Vector3(0f, anim.GetFloat("ColliderY"), 0f);
//}
else
{
grounded = false;
jump = false;
fall = true;
if (fall)
{
Debug.Log("Falling");
//controller.height = anim.GetFloat("ColliderHeight");
}
}
anim.SetBool("Grounded", grounded);
anim.SetBool("Jump", jump);
anim.SetBool("Fall", fall);
}
if (isChangingLane)
{
if (Mathf.Abs(transform.position.x - locationAfterChangingLane.x) < 0.1f)
{
isChangingLane = false;
moveVector.x = 0f;
}
}
MoveLeftRight();
verticalVelocity -= gravity * Time.deltaTime;
moveVector.y = verticalVelocity;
// Z - Forward and Backward
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
What am I doing wrong. How do I correctly configure. Help would be appreciated.
Your answer
Follow this Question
Related Questions
how to create a repeating environment in a 3D endless runner game 0 Answers
Efficient way to instantiate a sprite that plays an animation then destroys itself? (3d space) 1 Answer
Door animation bugs and stays open unity 3D 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers