Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Haugkall · Mar 08, 2018 at 09:38 PM · jumpingcoroutines

Disabling jump for 0.25 sec after you hit the ground after jumping?

It's a minor thing but I wouldn't want the character to stand on the spot and jump endlessly, a pause in between the jumps would be nice. I thought I might be able to disable the jump using a coroutine but it doesn't seem to work. Any more experienced programmer see the obvious problem?

I guess I might also ask why the sound of the character jumping sometimes happens twice. I guess when he is jumping he is touching the ground for more than one frame. Not sure how to solve this, I also find it odd that I can't multiply rotation by delta time. Are there any obvious solutions to any of these issues? :)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Character : MonoBehaviour {
 
     //Todo
     //•How to multiply rotation by deltaTime?
     //•Why can't I give the jump a float?
     //•The jump sound is played twice?
 
     Transform playerTransform;
     Rigidbody playerRigidBody;
 
     public AudioSource Jump;
     public Text Gametimer;
 
     public float characterFSpeed = 4.5f;
     public float characterBSpeed = 2f;
     public float jumpHeight = 150f;
     public bool touchesGround;
     public bool inTheAir;
     public bool jumpEnabled;
 
     void Start () {
 
         playerTransform = GetComponent<Transform>();
         playerRigidBody = GetComponent<Rigidbody>();
         playerRigidBody.freezeRotation = true;
         jumpEnabled = true;
     }
     
     void Update () {
 
         //Timer
         Gametimer.text = "Time: " + Time.realtimeSinceStartup.ToString();
 
         //Player rotation, movement and jump.
         playerTransform.Rotate(0, Input.GetAxis("Horizontal"), 0);
 
         Vector3 forward = new Vector3(0f, 0.0f, Input.GetAxis("Forward"));
         transform.Translate(forward * characterFSpeed * Time.deltaTime, Space.Self);
 
         Vector3 backward = new Vector3(0f, 0.0f, Input.GetAxis("BackUp"));
         transform.Translate(backward * characterBSpeed * Time.deltaTime, Space.Self);
 
         if (Input.GetButton("Jump") && touchesGround && jumpEnabled)
         {
             touchesGround = false;
             Jump.Play();
             inTheAir = true;
             StartCoroutine(co());
             playerRigidBody.AddForce(0f, 150f, 0f);
         }
 
     }
 
     IEnumerator co()
     {
         if (inTheAir && touchesGround)
         {
             Debug.Log("Detected");
             jumpEnabled = false;
             yield return new WaitForSeconds(5f);
             jumpEnabled = true;
             inTheAir = false;
         }
 
     }
 
 
     void OnCollisionStay()
     {
         touchesGround = true;
         Debug.Log("Touching the ground.");
     }
 }
Comment
Add comment · Show 4
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 grimmdeveloper · Mar 09, 2018 at 04:09 AM 1
Share

You're using GetButton, when you should be using GetButtonDown, to prevent it playing twice, also in your IEnumerator co() change the 5f in your WaitForSeconds to 0.25f, as 1f is a second in unity.

avatar image Haugkall grimmdeveloper · Mar 09, 2018 at 07:05 AM 0
Share

Oh yes that fixed the sound issue, however when I lowered the seconds in the coroutine now he's given a double jump somehow. Even though he is only suppose to be able to jump if he is touching the ground.(touchesGround == true) It actually doesn't even print "Detected" to console, so it seems the coroutine isn't even started, very odd.

But it might be hard to spot the issue without having the files on hand so thanks for resolving my jump issue. I'll see if I can't mess around with this a bit further and figure out how to do it correctly.

avatar image meat5000 ♦ Haugkall · Mar 09, 2018 at 10:28 AM 0
Share

$$anonymous$$y brain struggles with your logic of being in the air and touching the ground at the same time.

Show more comments

1 Reply

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

Answer by grimmdeveloper · Mar 09, 2018 at 10:40 AM

Add another condition to the if getbuttondown("jump") add a condition to check if intheair == false.

Comment
Add comment · Show 13 · 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 Haugkall · Mar 09, 2018 at 11:03 AM 0
Share

I solved the issue by adding a OnCollisionExit() a whole lot cleaner and now he jumps around without jumping twice. Also I find that the jumping somehow takes a bit longer to respond upon hitting the ground again. So there is no need for a delay after hitting the ground after having just jumped. Sound and jump issues solved, I got another issue with my self made axises not detecting the xbox joystick but I'll make another question for that unless there's an easy fix that you know of, cheers.

     void Update () {
 
         //Timer
         Gametimer.text = "Time: " + Time.time.ToString();
 
         //Player rotation, movement and jump.
         playerTransform.Rotate(0, Input.GetAxis("Horizontal"), 0);
         Vector3 forward = new Vector3(0f, 0.0f, Input.GetAxis("Forward"));
         transform.Translate(forward * characterFSpeed * Time.deltaTime, Space.Self);
 
         Vector3 backward = new Vector3(0f, 0.0f, Input.GetAxis("BackUp"));
         transform.Translate(backward * characterBSpeed * Time.deltaTime, Space.Self);
 
         if (Input.GetButtonDown("Jump") && touchesGround)
         {
             Debug.Log("Jumping");
             Jump.Play();
             playerRigidBody.AddForce(0f, 325f, 0f);
         }
         else
         {
         }
 
     }
 
     void OnCollisionStay()
     {
         touchesGround = true;
         Debug.Log("Touching the ground.");
     }
 
     void OnCollisionExit()
     {
         touchesGround = false;
         Debug.Log("Not touching the ground");
     }
 }
avatar image grimmdeveloper Haugkall · Mar 09, 2018 at 11:16 AM 0
Share

Input.GetAxis("Vertical"); ins$$anonymous$$d of Forward and BackUp. Vertical axis is used for up and down on the left joystick and horizontal is used for left and right on the same joystick.

avatar image Haugkall grimmdeveloper · Mar 09, 2018 at 11:27 AM 0
Share

Yes I know but I want different speeds depending on if he is moving forward or backing up. I managed to do that by creating two axises, it's working with the keyboard but it's not detected by the x box controller. :p

Show more comments

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

129 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

vibrating jumps and collisions 0 Answers

Touch control swipe up to jump 1 Answer

My game seems to be running too fast for certain tasks 0 Answers

problem with jump script 2 Answers

Help please! Why can't I reduce the speed before jumping? 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