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 /
avatar image
0
Question by AdminBenni · Nov 07, 2016 at 03:02 PM · c#rigidbodyspeed

Getting speed without Rigidbody.velocity.magnitude

Hi there! I'm working on a game where the longer the player stays moving the more they accelerate and I made this code to to check if player is moving above a certain speed.

 void FixedUpdate ()
     {
         if (player.velocity.magnitude >= 2)
         {
             print("true" + player.velocity.magnitude);
             player.AddRelativeForce(Vector3.forward * 1.5f * Time.deltaTime);
         }
         else
         {
             print("false" + player.velocity.magnitude);
         }
     }

However player.velocity.magnitude always returns zero (I've also checked if player is equal to the correct Rigidbody and it is)

I searched a bit online and apparently it's something about the FPCharacter altering the speed directly so the Rigidbody doesn't know the speed, yet none of them gave an alternative of checking the speed. Is there even a way to do this?

All help appreciated :D

Comment
Add comment · Show 1
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 TreyH · Nov 07, 2016 at 03:30 PM 1
Share

You can just do this numerically. Speed is given in units of distance over time, so you'll want to make a similar calculation. There are more accurate ways to do this, but the simplest (which will be expanded a bit) would just be:

 private Vector3 previousPosition;
 
 // Use this for initialization
 void Start () 
 {
     previousPosition = this.transform.position;
 }
 
 // Physics routine
 void FixedUpdate()
 {
     // Test it
     Debug.Log("Est.Speed: " + this.EstimatedSpeed( Time.fixedDeltaTime).ToString("F1"));
 }
 
 private float EstimatedSpeed(float timePassed)
 {
     // For completeness, we'll get the velocity vector itself, which is the distance travelled
     // in each direction divided by the time passed.
     Vector3 velocityVector = (this.transform.position - this.previousPosition) / timePassed;
 
     // Get the magnitude of thie vector
     float velocity$$anonymous$$agnitude = velocityVector.magnitude;
 
     // $$anonymous$$ark this position for next time
     this.previousPosition = this.transform.position;
 
     // Then just return what we calculated
     return velocity$$anonymous$$agnitude;
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AurimasBlazulionis · Nov 07, 2016 at 04:25 PM

You need to store position of the object one frame before and use some something similar like this (transform.position - oldPosition).magnitude / Time.deltaTime or fixedDeltaTime if you wish to do everything in FixedUpdate.

Few notes:

  1. The speed you get is in world space. You will probably need to do Mathf.Abs(theSpeedCalculation) to get it without negative numbers. And also, you might need not to take Y axis in to account. To do that, just set the Y position of the old position to the current Y position.

  2. normalized speed calculation is quite expensive. It computes a cubic root everytime. If you just need to check if the speed is higher than specific value and do not need to use speed in other calculations, you can use sqrMagnitude instead of magnitude to get a non cubic rooted value. Then compare speed to the value cubed or multiplied 3 times together. This will achieve what you need.

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 TreyH · Nov 07, 2016 at 06:47 PM 0
Share
  1. Norms give positive numbers, there is no need to take the absolute value of a magnitude, as it takes the square root of squared real numbers.

  2. Normalization is a square root, as distance in R3 (the world Unity defaults to) is an L2 norm.

avatar image AurimasBlazulionis TreyH · Nov 08, 2016 at 10:36 AM 0
Share

Thanks for pointing out a mistake, I meant magnitude ins$$anonymous$$d of normalized.

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

6 People are following this question.

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

Related Questions

Increase speed of an object's fall 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to limit an object's position to a spherical radius? 0 Answers

Why does my rigidbody slows down at time ? 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