Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Greathos · Jan 12, 2014 at 10:51 AM · c#waitforsecondsstartcoroutine

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hoeloe · Jan 13, 2014 at 10:34 AM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Greathos · Jan 12, 2014 at 05:55 PM 0
Share

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;
     }
     }
     }
avatar image RudyTheDev · Jan 12, 2014 at 09:23 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges