- Home /
How can i add different jumping state in unity3d
i need to add different-different jumping state in my animator controller and how can i call all the jump state by pressing space key in the keyboard. I mean jump will come randomly.
Answer by darthtelle · Apr 23, 2014 at 01:10 PM
I'm assuming you have multiple animation states already set up in your animator controller, for example, Jump_01 or Jump_02. And what you want is when you press a button or key to jump, a random animation is played?
If so, one way of doing so would be to have a different trigger parameter which transitions to each jump animation. In the code, you would store these parameter names, possibly in an array, and when jump is triggered, you would choose a random parameter from your array and tell the animator to trigger that parameter.
Pseudo Code:
string[] animParameters = { jump_01, jump_02 }; // Trigger Parameters
void OnJump()
{
int randomJumpAnim = Random.Range(0, animParameters.Length); // Choose a random parameter from the array
gameObject.GetComponent<Animator>().SetTrigger(animParameters[randomJumpAnim); // Tell the animator to trigger it
}
thanks for reply. but how can i call the animator parameter in string[] animParameters. when i call the parameter in string array i got error. Error is.... The name jump_01' does not exist in the current context The name
jump_02' does not exist in the current context
Thanks
When you have your object you wish to animate selected, in the Animator window, your Animator Controller should be displayed. All the animations you have assigned your object should be in here. In the bottom left corner is a box full of parameters, these are used to transition to your animations. Do you have these set up? These are the names you should be storing inside the string array.
random jump working fine while i press the jump button its come randomly but after finish the one jump other jump took place automatically without pressing jump button.how can i solve this problem.
You're probably best off starting a new question and posting your code. Without code, I can't help you specifically!
Here is my code:
string[] animatorparameters = {"jump_01","jump_02"};
void OnJump()
{
int randomJumpAnim = Random.Range(0, animatorparameters.Length); // Choose a random parameter from the array
//animator.SetTrigger (animatorparameters [randomJumpAnim]);
print (animatorparameters [randomJumpAnim]);
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Base Layer.Run"))
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
animator.SetTrigger(animatorparameters[randomJumpAnim]);
}
}
}