- Home /
Allowing only one jump?
I am attempting to allow an animation to only jump once each time the "Jump" button is pressed and also stop the animation from staying at the top of the "Jump" animation if the the button is held.
using UnityEngine;
using System.Collections;
public class BasicCharacterControl : MonoBehaviour
{
public Animator animator;
public float rotSpeed = 90f;
public bool canJump = true;
public float j;
void Start ()
{
animator = GetComponent<Animator>();
}
void Update ()
{
}
void FixedUpdate()
{
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
j = Input.GetAxis ("Jump");
animator.SetFloat ("V Input", v);
animator.SetFloat ("Turning", h);
transform.Rotate(new Vector3(0, h * Time.deltaTime * rotSpeed, 0));
StartCoroutine (ProcessJump ());
}
IEnumerator ProcessJump()
{
This is where the Jump starts
if (j == 1 && canJump == true)
{
animator.SetBool ("Jump", true);
This is what should stop the jump and not allow it to start again until you let go of the "Jump" button
yield return new WaitForSeconds(1);
animator.SetBool ("jump", false);
canJump = false;
print (canJump);
}
else if (j == 0)
{
yield return new WaitForSeconds(1);
canJump = true;
}
}
}
I first attempted to do this without the WaitForSeconds and ran into a similar issue. After reading it seemed this would be a great way to do this. It seems I am still missing a concept here.
Answer by Greathos · Jan 13, 2014 at 06:17 AM
Figured it out. A syntax error was stopping stopping the code from moving forward.
IEnumerator ProcessJump() { if (j == 1 && canJump == true) { animator.SetBool ("Jump", true);
yield return new WaitForSeconds(1); "jump" should be "Jump"
animator.SetBool ("jump", false);
canJump = false;
}
else if (j == 0)
{
yield return new WaitForSeconds(1);
canJump = true;
}
}
}
Thank you for the help
Just feel that I should point out that that's not a syntax error. It's a typo, yes, but the syntax is the grammatical structure of the program, and that is correct. Programs can be syntactically correct but not work properly, or have other errors. I'm glad you fixed your problem, but I see no harm in spreading a bit of bonus knowledge around!
Answer by RudyTheDev · Jan 12, 2014 at 12:47 PM
If I understand you correctly, simply trigger your jumping code when the button is pressed down and not held. Kinda like Input.GetButtonDown, but you need to write your own version since Axis don't have that. See this answer for example.
Following this example yields the same result. Your right I am trying to have it only work when pressed down and not held. But from what I am reading about "WaitForSeconds" it should stop what it is doing, wait, than execute the next line.
So here when the jump starts it waits for a frame, and runs the next line that stops the jump even though the button is pressed and because it is a two part conditional and I set one of the conditions to false as long as the button is held it shouldn't start the "jump" again until you let go and press it again.
IEnumerator ProcessJump()
{
//This is where the Jump starts
if (j == 1 && canJump == true) { animator.SetBool ("Jump", true);
This is what should stop the jump and not allow it to start again until you let go of the "Jump" button
yield return new WaitForSeconds(1); animator.SetBool ("jump", false); canJump = false; print (canJump);
}
else if (j == 0)
{
yield return new WaitForSeconds(1);
canJump = true;
}
}
}
You don't need coroutines for this. Simply remember what the last state of the jump button was. And only trigger jump if last state was up and current state is down.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Another question about yield, WaitForSeconds in c# 2 Answers
Distribute terrain in zones 3 Answers
C# WaitForSeconds doesn't seem to work ?? 2 Answers
Using WaitForSeconds and trigger 1 Answer