- Home /
control animation using animation control class
I did a sample existing code to control animation using animation.I dragged and dropped a character into the scene then I added a Animator to it and added the animator controller.Below I have given the screen short of the animation
Then I have attached the following code to it
Animator animator;
int eatHash=Animator.StringToHash("Jump");
int stateHash=Animator.StringToHash("Base Layer.idle");
void Start () {
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
//animator.SetBool ("isEat", true);
float move = Input.GetAxis ("Vertical");
animator.SetFloat ("Speed",move);
AnimatorStateInfo stateinfo = animator.GetCurrentAnimatorStateInfo (0);
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log ("clicked spacebar");
animator.SetTrigger(eatHash);
}
}
The problem here is the above code is working,when I run the project the animation is getting played one after the another according to the state diagram.But when I click on the space bar the trigger function(jump animation ) is not working
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log ("clicked spacebar");
animator.SetTrigger(eatHash);
}
Answer by HarshadK · Sep 09, 2014 at 01:11 PM
The Jump state has a transition only from idle state. So on pressing Jump the jump state animation will only be played if the character is currently in idle state.
If you want to transition from any other state to jump state then you need to create such transitions between stated in the animator.
@Harshad : Thanks a lot for the valuable reply.Can you help me how to create transition between states.You mean whether i need to add transition form all the states to the jump state.
Create a transition from 'Any State' to your jump state.
Watch this s$$anonymous$$lth tutorial for PLAYER ANI$$anonymous$$ATOR CONTROLLER.
From the above transition diagram i have removed the transition link between each state and only given link between Any State and idle.This will work fine?
@Harshadk : I made the transition from Any State to jump but still not working
You also need to make a transition from your current states to Any State.
Also, did you check the s$$anonymous$$lth video tutorial I provided above?