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 lmakor · Sep 01, 2018 at 04:05 AM · positioncharactercontrollervelocity

Predicting Future Rigidbody position based on velocity

Currently I am trying to understand some about the physics concepts of unity and therefore I am implementing a simple Character Controller.

I am trying to predict the character position after the next physics update based on the set velocity. Here is the code I am currently using:

 public class MyCustomCharacterController : MonoBehaviour
 {
 
     private float moveSpeed = 5.0f;
     private Rigidbody rigidbody;
 
     //input constants
     private int horizontalMovementDirection;
     private int verticalMovementDirection;
 
     // Use this for initialization
     void Start()
     {
         rigidbody = GetComponent<Rigidbody>();
         rigidbody.freezeRotation = true;
     }
 
     private int getHorizontalMovementDirection()
     {
         int direction = 0;
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             direction--;
         }
         if (Input.GetKey(KeyCode.RightArrow))
         {
             direction++;
         }
         return direction;
     }
 
     private int getVerticalMovementDirection()
     {
         int direction = 0;
         if (Input.GetKey(KeyCode.DownArrow))
         {
             direction--;
         }
         if (Input.GetKey(KeyCode.UpArrow))
         {
             direction++;
         }
         return direction;
     }
 
     private void FixedUpdate()
     {
         float velX = moveSpeed * getHorizontalMovementDirection();
         float velZ = moveSpeed * getVerticalMovementDirection();
         Vector3 velocityVector = new Vector3(velX, 0, velZ);
         rigidbody.velocity = velocityVector;
         Vector3 rbPos = rigidbody.transform.position;
         if (velocityVector.x != 0 || velocityVector.y != 0 || velocityVector.z != 0)
         {
             Debug.Log("curPos: " + rbPos.ToString("F6"));
             Debug.Log("vel: " + velocityVector + " - calculated position: " + (rbPos + (velocityVector * Time.fixedDeltaTime)).ToString("F6"));
         }
 
     }
 
     // Update is called once per frame
     void Update()
     {
         //save input
         horizontalMovementDirection = getHorizontalMovementDirection();
         verticalMovementDirection = getVerticalMovementDirection();
     }
 }
 

The output I get is like the following:

 curPos: (3.000000, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.900000, 0.524603, 3.000000)
 curPos: (2.904709, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.804709, 0.524603, 3.000000)
 curPos: (2.809418, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.709418, 0.524603, 3.000000)
 curPos: (2.714127, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.614127, 0.524603, 3.000000)
 curPos: (2.618835, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.518836, 0.524603, 3.000000)
 curPos: (2.523544, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.423544, 0.524603, 3.000000)
 curPos: (2.428253, 0.524603, 3.000000)
 vel: (-5.0, 0.0, 0.0) - calculated position: (2.328253, 0.524603, 3.000000)


As you can see the first prediction is off by 0.004709 and all other calculations are correct (the error from the first calculation is just pulled through to the end) NOTE: everytime the rigidbody starts moving, after standing still, that error occurs again.

Does anyone know why this happens? Or am I making an obvious mistake?

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 JVene · Sep 01, 2018 at 05:37 AM

An exact explanation requires an examination of the source code to the physics engine, which you can actually obtain. Unity uses PhysX, and if you're using a recent version of Unity (likely), it's probably PhysX 3.3 (I'd have to double check that).


However, an exact answer will be a bit unsatisfying, in part because it would take such a long time to research for what amounts to a really simple, if generalized, answer.


The documentation on Rigidbody.velocity includes a warning:


"In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation. A typical example where you would change the velocity is when jumping in a first person shooter, because you want an immediate change in velocity."


I've worked on some open source physics engines as a contributor, though not PhysX. The exact reason PhysX is adding that precise value I can't answer, but what I can tell you is that when changing velocity you're basically injecting a value into an engine that is basically in mid-calculation from one moment to the next, and it isn't a direct control. It is not, as the name implies and you'd probably expect, an exact and instantaneous change of the object's velocity (which is also implied by the experiment you're performing). This velocity value is really intended as a read-mostly property (and they hope read only), but on the occasion you might want some special effect this can perform, using it is known to produce non-realistic results because the engine 'expects' (or is designed) to take a force and move the object, whereas this is applying an abrupt (I'll avoid the word instantaneous here) change in velocity that otherwise would not have an actual physical explanation. Using velocity to control an object is a bit of kludge or fudge, so it won't work with precision. Since, as you notice, the rest of your calculations work, your test is actually a success, given that the engine isn't expected to "like" being poked this way ;)

Comment
Add comment · 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

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

179 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 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

Using Velocity breaks my jumping 0 Answers

How to stop cube from sliding (rg2d.velocity) 1 Answer

Moving rigidbody with addforce, velocity or position causes another object not to collide anymore. 0 Answers

Ethan unable to leave a position on the scene 2 Answers

Weird Z position behavior 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