- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
abhishakverma · May 06, 2014 at 10:00 AM ·
c#
How can i add the random jump?
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.
code here
using UnityEngine;
using System.Collections;
public class jump: MonoBehaviour {
string[] animatorparameters = {"jump_01","jump_02"};
protected Animator animator;
void Start ()
{
animator = GetComponent<Animator>();
}
Void Update(){
OnJump();
}
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.GetKey(KeyCode.Space))
{
animator.SetTrigger(animatorparameters[randomJumpAnim]);
}
}
}
}
Comment
Best Answer
Answer by Nerevar · May 06, 2014 at 10:08 AM
Hello,
May be try to add this condition:
if (stateInfo.IsName("Base Layer.Run"))
{
if(Input.anyKeyDown){
if (Input.GetKey(KeyCode.Space))
{
animator.SetTrigger(animatorparameters[randomJumpAnim]);
}
}
}
regards
Your answer
![](https://koobas.hobune.stream/wayback/20220613144350im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Trigger player movement on touch 1 Answer
How to measure ad impressions in In-Game Ads? 0 Answers
Umlaute missing in Unity GUI. 1 Answer