• 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 latsushi · Jan 09, 2013 at 10:20 PM · jumpyielddelaycoroutines

Delay Jump

Hello Unity Community,

I'm trying to create a slight delayed jump at 1.3 seconds after the player has collided with specific objects. For example, when the user collides with certain objects that have a tag called "Collision" and then they jump, it should take 1.3 seconds before the player jumps.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class MoveForward : MonoBehaviour {
     public float speed = 6.0F;
     public float jumpSpeed = 8.0F;
     public float gravity = 20.0F;
     private Vector3 moveDirection = Vector3.zero;    
     
     void Start () {
         AkSoundEngine.PostEvent("Play_FS_Running", gameObject);    
     }
     
     // Update is called once per frame
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
          if (controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 2);
             moveDirection = transform.TransformDirection(moveDirection);
             moveDirection *= speed;
              if (Input.GetButton("Jump"))
             {
                 moveDirection.y = jumpSpeed;
                 AkSoundEngine.PostEvent("Play_Jump", gameObject);
             }
                 
         }
         moveDirection.y -= gravity * Time.deltaTime;
         controller.Move(moveDirection * Time.deltaTime);    
     }
     void OnControllerColliderHit (ControllerColliderHit OtherObj) {
         if (OtherObj.collider.tag == "Collision")
          Debug.Log("We've collided"); 
        }
 }

The line before 'Debug.Log("We've collided");' is where I need to put the proper code I believe. I've heard that I should look into coroutines but, from my understanding, a coroutine yields methods so it wouldn't work with my current code. Maybe I'm wrong. What I need is to be pointed in the right direction. Any input is appreciated as I've been struggling with t$$anonymous$$s problem for almost 2 months. Seriously.

Thank you for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pad406 · Jan 09, 2013 at 10:35 PM

You're right about the coroutine but I t$$anonymous$$nk you have to split it into a new routine. So at your debug.log statement do t$$anonymous$$s

 startcoroutine(myDelay());

Then your myDelay function as follows:

 //myDelay function
 IEnumerator myDelay()
 {
 yield return WaitForSeconds(1.3f);
 //Whatever code you want to do, ie jump
 }

Alternative would be use Invoke

 Invoke("MyJumpRoutine",1.3F);
 
Comment
Add comment · Show 5 · 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 latsushi · Jan 09, 2013 at 11:11 PM 0
Share

I'm getting an error on the IEnumerator line that says:

error CS1525: Unexpected symbol `('

Here's my code:

void OnControllerColliderHit (ControllerColliderHit OtherObj) { if (OtherObj.collider.tag == "Collision"){

         StartCoroutine(myDelay());
         
     IEnumerator myDelay(){
             
     yield return WaitForSeconds(1.3f);
             if (Input.GetButton("Jump"))
             {
             moveDirection.y = jumpSpeed;
             AkSoundEngine.PostEvent("Play_Jump", gameObject);
             }
             
         }
    }
 }
avatar image latsushi · Jan 09, 2013 at 11:13 PM 0
Share

Also, I'm not sure if the code to jump (lines after if statement) is correct. :/

Is there a simpler way to put in code to jump after the yield statement? I can tell I'm doing something wrong.

avatar image pad406 · Jan 09, 2013 at 11:44 PM 0
Share

First the myDelay is a seperate routine/function. You can't put it inside the OnControllerCollierHit. Next what you are basically doing there is saying 'When I collide, wait for 1.3 seconds and then if the Jump button is pressed, jump. I'm not sure what you are trying to achieve with both the collider and the Jump. Is the player not allowed jump till 1.3s after they collide or after they hit the jump button? Can they only hit the jump button after they collide? The code will be slightly different depending on the 'rule' your're trying to implement

avatar image latsushi · Jan 10, 2013 at 12:07 AM 0
Share

The player is not allowed to jump till 1.3s after they hit the jump button.

avatar image pad406 · Jan 10, 2013 at 09:09 AM 0
Share

Ok, why not then, at the point where the player is allowed jump, do the following:

if (Input.GetButton("Jump")) { Invoke("MyJumpRoutine", 1.3F); }

Then have a routine called MyJumpRoutine which has the code you want for the jump.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Making a character jump with the Character Controller Component instead of Capsule Collider 0 Answers

Can't Modify PlatformerCharacter2D Jump Force From Other Script 0 Answers

How to add delay before showing GUI Button? 3 Answers

Mysteries of yield 1 Answer

Using coroutine as Update(). Waiting until the button is pressed. 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