- Home /
How come my player animation won't play?
I have a model and I already have the walk and Idle animation to play but somehow I can never get the "Jump" to play. I really can't figure why?
I used tried triggers but still no good.
here is my code on my model called "AnimatePlayer":
#pragma strict
var body : Transform; //Tranform the armature of the player model
var mousexThreshold = 0.2; //Number of units for the mouse to move on x-axis
//Settings for the Upper Body rotation
var sensitivityY = 15F;
var minimumY = -60F;
var maximumY = 60F;
var rotationY = 0F;
var jumpBool : boolean;
function Start()
{
//set animation Wrap Mode for the attack to "Once"
animation["Jump"].wrapMode = WrapMode.Once;
}
function Update () {
if( transform.rotation.eulerAngles.y == 270 ) { // Left rotation
if(Input.GetAxis("Horizontal") > 0) //Walk Backwards
{
animation["Walk"].speed = -2;
animation.Play("Walk");
}
else if(Input.GetAxis("Horizontal") < 0) //Walk Forward
{
animation["Walk"].speed = 2;
animation.Play("Walk");
}
else if(Input.GetAxis("Horizontal") == 0)
{
animation.Play("Idle");
}
}
if( transform.rotation.eulerAngles.y == 90.00001 ) { // Right rotation
if(Input.GetAxis("Horizontal") > 0) //Walk Forward
{
animation["Walk"].speed = 2;
animation.Play("Walk");
}
else if(Input.GetAxis("Horizontal") < 0) //Walk Backwards
{
animation["Walk"].speed = -2;
animation.Play("Walk");
}
else if(Input.GetAxis("Horizontal") == 0) //Return to standing posture
{
animation.Play("Idle");
}
}
// Transform the rotation of the player when mouse moves on the x-axis
var mousex : float = Input.GetAxis ("Mouse X");
if (mousex > mousexThreshold) //Player faces right
{
transform.forward = new Vector3(1f, 0f, 0f);
}
else if (mousex < -mousexThreshold) //Player faces left
{
transform.forward = new Vector3(-1f, 0f, 0f);
}
}
function LateUpdate(){
// will make the upper body of the model to be rotatable along the y-axis
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
body.transform.localEulerAngles = new Vector3(0, rotationY, 0);
}
function OnTriggerEnter(other : Collider){
if(other.tag == "Ground"){
animation.Play("Jump");
}else if(other.tag == "Platform"){
animation.Play("Jump");
}
}
function OnTriggerExit(other : Collider){
if(other.tag == "Ground"){
animation.Play("Idle");
}else if(other.tag == "Platform"){
animation.Play("Idle");
}
}
As you can see, I have a trigger to play the "Jump" animation but still nothing. Is it because I have a lot of animations playing all at once?
Put a Debug.Log
message before jump animation call to see if it ever gets triggered.
Answer by JonS · Oct 31, 2014 at 07:53 PM
Looks like two things:
First, I think you have your triggers backwards. You're trying to play the Jump animation when the player lands and playing the Idle animation when he leaves the ground.
Second, you're playing the Walk and Idle animations based on the Horizontal input in Update without any checks to see if he's currently jumping. Even if you play the jump animation properly, Update will immediately play Idle or Walk again the next frame and overwrite the jump.
You need to keep track of when your character is in the air and not call the Horizontal input checks when he is. Ideally you shouldn't be calling animation.Play() constantly in the Update like that. Keep track of what state your character is in, eg Attacking, Jumping, Walking, and only call animation.Play() when he actually changes between states.
ohh ok, yeah the trigger I figured out already haha
so I research and found another animation command called "animation.CrossFade()".
and my other question is, when you mean by checks do you mean "boolean"?
Your answer
Follow this Question
Related Questions
How do I get my "dashing" animation to work? 1 Answer
Why isn't my animation being played? 0 Answers
Changing number of frames in uvAnimationTileX 0 Answers
Using animationState for animation controls 2 Answers
how character's legs(tps) move? 0 Answers