• 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
Question by andypan · Oct 14, 2015 at 09:51 AM · rigidbodyvelocityjumping

Detect if velocity equals 0?

Hi Unity Answers!

I have a basic jump working for my Movement script. However, I want the player to only be able to jump if they're on the ground. I've used different methods that check for ground collision and even Raycast distance from ground...but they all have flaws.

My new solution is to check whether if player's "y" velocity is 0 then they're able to jump. Problem is I can't figure out how to write that in C#.

Here's my current working jump script without the velocity detection:

 public class NewBehaviourScript : MonoBehaviour {
     public Rigidbody PlayerRB;
     public float jumpHeight = 5f;
     void Start ()
     {
         PlayerRB = GetComponent<Rigidbody>();
     }
 
     void Update ()
     {
         if (Input.GetButtonDown("Jump"))        
             PlayerRB.velocity = new Vector3(0, jumpHeight, 0);
     }
 }
 

I've been trying to write something like this to detect the player's velocity

     void Update ()
     {
          if (Input.GetButtonDown("Jump")) && PlayerRB.velocity == 0);        
             PlayerRB.velocity = new Vector3(0, jumpHeight, 0);
     }

I've added (PlayerRB.velocity == 0) but this gives me errors. I've tried different ways of writting it...but I'm very new to C# so I really don't have a clue. Thanks guys!

Comment
EvanCheddar

People who like this

1 Show 0
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
Best Answer

Answer by Dave-Carlile · Oct 14, 2015 at 02:13 PM

Velocity is a Vector3. Among other things it has fields for x, y, z. This is why when you create a vector you code things like PlayerRB.velocity = new Vector3(0, jumpHeight, 0);. The first 0 is the x vector component, jumpHeight is y, and the final 0 is z.

You can examine one of those fields like this...

 float y = PlayerRB.velocity.y;

That gives you the value for y. Then you can compare it just like any other float variable. You can also just check it directly...

 if (PlayerRB.velocity.y == 0)
 {
    ... do something ...
 }

Now, generally you don't want to compare floating point values for equality. Because of precision errors the value might be something like 0.0000001. This is effectively 0 when it comes to speed, but if you try to see if it's equal to 0 that will be false. A better way to do this comparison is to use the Mathf.Approximately function to do the comparison...

 if (Mathf.Approximately(PlayerRB.velocity.y, 0))
 {
 }

This will compare the value of velocity.y to 0 and return true if it's closer than some very small value.

Comment
andypan
igorskugar
EvanCheddar
OmerPasa3328

People who like this

4 Show 0 · 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

Answer by GaryOPostle · Jun 21, 2017 at 10:04 PM

I know this question is old but I just googled it myself and then found out the answer so, you can just compare the velocity to Vector3.zero;

void Update () { if (Input.GetButtonDown("Jump")) && PlayerRB.velocity == Vector3.zero);
PlayerRB.velocity = new Vector3(0, jumpHeight, 0); }

Comment

People who like this

0 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 Dennis_eA · Aug 07, 2017 at 02:27 AM 0
Share

You should Never compare a velocity (or anything) directly to vector3.zero (similar like checking if some float is == 0.0.. no!) Also, you are manipulating the velocity of your rigid body directly, from the docs:

"In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour"

Also; do it in FixedUpdate !

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

7 People are following this question.

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

Related Questions

Rocket Landing 1 Answer

Make a "Balloon" that floats upwards but slows as it gets higher 1 Answer

Take an average of the velocity and angular velocity of Parent over the last few frames and set directly on the Child Rigidbody at the moment the object is released. 0 Answers

RigidBody,velocity not working on Start method 1 Answer

Slow down a character while they are midair while keeping their original velocity 0 Answers


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