Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by H4nsi_Kursch · Feb 11, 2015 at 01:13 PM · physicsunity 2dvelocityphysics2dplatformer

Predict next rigidbody position based on velocity

I'm making a 2D platform game and I'm moving the player applying a velocity on the rigidbody2D. However, I have a situation where I need to be able to calculate the next "x" position of the rigidbody2D on the next frame in order do trigger something in the game.

Is there a way to calculate that based on the velocity? I'm currently applying the velocity using the following code:

rigidbody2D.velocity = new Vector2(MOVE_SPEED * Time.deltaTime, rigidbody2D.velocity.y);

I've tried to calculate MOVE_SPEED * Time.DeltaTime and add it to the current x position of the player but It doesn't work. The value is different from the actual next position.

Does anybody knows how can I do that?

Thank you!

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
8
Best Answer

Answer by hav_ngs_ru · Feb 11, 2015 at 03:38 PM

hwww...

First - should your rigidbody move evenly, with constant speed? If so - you should not use deltaTime to calculate speed, just constants. Otherwise object could twitch some times. just like this

 // to leave y-speed as it was:
 rigidbody2D.velocity.y = MOVE_SPEED;

 // or to make object move only to the right:
 rigidbody2D.velocity = Vector2.right * MOVE_SPEED;

Second... distance = velocity x time, and point2 = point1 + path... so:

 Vector2 predictedPoint = new Vector2(transform.position.x, transfomr.position.y) + rigidbody2D.velocity * Time.deltaTime;

but keep in mind that previous deltaTime could be not the same as next deltaTime if you use Update for calculations... so prediction could be not very accurate in this case... if you use FixedUpdate instead - AFAIK you will have equal deltaTime for every call, and you will predict exatly right point.

Comment
Add comment · 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 H4nsi_Kursch · Feb 11, 2015 at 04:20 PM 0
Share

Thank you! It worked :-)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Velocity from external objects 0 Answers

how to get starting velocity of an object, which is thrown with a 45 degree angle and will pass some (x,y) point 0 Answers

Need help with my movement script.. :( 1 Answer

Velocity Movement & Physics Interactions by Rigidbody2D 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