• 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 CamoLeopard · Jul 10, 2016 at 12:03 PM · physicsmath

Get Rigidbody Time to Destination.

Hey how's it going,

I am working on a physics based game and I have recently gotten some code that will rotate my character based on how long it is in the air and it will always land on a flat side, but to do this, I need to calculate the time to the destination. Because I am setting the velocity of the via

 rigidBody.velocity = new Vector3(rigidBody.velocity.x,jumpForce*jumpForceMultiplier,moveSpeed);

is there anyway to calculate the time it will take for the object to hit the ground? I have a ground check method that is just raycasting to the collider bounds +0.1 to check of the player is on the ground, if that helps.

Thanks.

Comment
Add comment · Show 8
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 CamoLeopard · Jul 11, 2016 at 03:13 AM 0
Share

Just to clarify because I believe I was unclear. How can I calculate where the player will land when they either go off of a ledge OR jump based on the velocity, AND how long it will take them to get there?

Sorry if I was unclear

avatar image Arkaid · Jul 11, 2016 at 03:19 AM 0
Share

That is not easy to calculate, AFAI$$anonymous$$. Depends on the trajectory of the player (straight line or parabolic), how far the colliders are from the player, what's the player initial velocity, gravity, drag, ter$$anonymous$$al velocity. And even with all of that, you're still not ensured you'll get an accurate result because physics in Unity are an approximation at best.

avatar image CamoLeopard Arkaid · Jul 11, 2016 at 03:54 AM 0
Share

Thanks Arkaid, here is some background information on the game, it is a 2.5d game, the camera angle is fixed but is using 3d graphics, so the trajectory will always be straight, and other than that, is there a way to make my player 'jump' so that I can know how long it will be in the air?

avatar image Arkaid CamoLeopard · Jul 11, 2016 at 04:02 AM 0
Share

There are some equations to calculate time of an object in freefall, so I'd start looking there. But I think simple is better: If the length of the jumps are more or less the same all the time, just measure the time it takes to make them ;)

Show more comments
avatar image _Yash_ · Jul 11, 2016 at 04:45 AM 0
Share

Is it possible to calculate the time taken if you have the starting position, a target position and the velocity?

 vector direction = end - start;
 vector distance = magnitude(direction);
 float speed = magnitude(velocity);
 float time = distance / speed;


avatar image CamoLeopard _Yash_ · Jul 11, 2016 at 05:52 AM 0
Share

Unfortunately this won't work because if I wanted to make the rigidbody actually move into the air, then I must set the target vector above where it needs to land and then it won't calculate when it will hit the ground correctly. Thanks. Feel free to correct me if i am taking this the wrong way.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Eno-Khaon · Jul 11, 2016 at 05:18 AM

A trajectory can be calculated at any given time. Your current velocity can be used to simulate upcoming physics to predict where you will hit the ground.

I included a large number of formulas in a previous answer, so they can be used in conjunction with Raycasts for a simple test for where the center of a collider would reach. The more accuracy and reliability you want, the more expensive the calculations will become.

Comment
Add comment · Show 18 · 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 CamoLeopard · Jul 11, 2016 at 05:50 AM 0
Share

Thanks alot for these, but how would I use them in my controller, if I want my character to go into the air, then I must set the y value to be above where I want it to and, which basically voids any attempts to calculate time because the target vector is not on the ground, and it will take more time for the object to fall onto the ground. Am I misinterpreting the answer?

avatar image Eno-Khaon CamoLeopard · Jul 11, 2016 at 06:17 AM 0
Share

Well, my previous answer didn't include absolutely everything...

The idea is that from any initial position with any initial velocity, you can deter$$anonymous$$e how long it will take to reach the ground by:

1) Recreating the physics timesteps.

 rigidbodyDrag = $$anonymous$$athf.Clamp01(1.0f - (rb.drag * Time.fixedDeltaTime)); // Per FixedUpdate()
 // This means that if your drag is equal to the framerate of FixedUpdate(), your object will lose 100% of its speed every frame

 velocityPerFrame = lastFrameVelocity + (Physics.gravity * Time.fixedDeltaTime);
 velocityPerFrame *= rigidbodyDrag;
 positionPerFrame += (velocityPerFrame * Time.fixedDeltaTime);

That is the general process by which the position is deter$$anonymous$$ed by the combination of physics velocity and drag.

If you already know your initial/current velocity (i.e. movement at any time whatsoever). you can simply use that as your starting point and simulate physics from there.

2) Check for the ground.

This is where the use of Raycasts comes into play. Fire a Raycast from the current point of calculation along its current velocity/frame distance (or from the previous point to the current one, if all the data's being retained). If the Raycast hits something, then you have the bare $$anonymous$$imum possible outcome for a collision. Again, more complex collision tests can improve accuracy at a much greater cost of calculation.

... And that's pretty much it.

It's really just a two-step process (Albeit a fairly complicated first step). The time when the collision will occur is deter$$anonymous$$ed by the number of physics timesteps which have been tested. For example, the default Time.fixedDeltaTime is 0.02, for a total of 50 physics calculations per second. With that in $$anonymous$$d, count the number of timesteps until the Raycast contacts the ground.

 // A very rough approach
 if(Physics.Raycast(positionPerFrame, velocityPerFrame.normalized, velocityPerFrame.magnitude * Time.fixedDeltaTime))
 {
     totalTime = elapsedTime;
 }
 else
 {
     elapsedTime += Time.fixedDeltaTime;
 }
avatar image CamoLeopard Eno-Khaon · Jul 11, 2016 at 06:30 AM 0
Share

Wow thanks alot for the really detailed response, I am just a little confused as to how I would put this into a single method for example. Sorry if I sound a bit confused, but I have been developing backend java applications for a while now and I am only just stepping back into Unity, C# and front end stuff in general. Thanks again for the amazing response.

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.

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

6 People are following this question.

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

Related Questions

Collision impact force 1 Answer

calculating the thrust duration to apply to a body to reach a certain height 2 Answers

Hop (Ketchapp) Mechanic (Jump between platforms with certain velocity while controlling object on X axis) 1 Answer

How to decrease/(have constant) speed of a ball rolling downhill? 0 Answers

Determining thruster activation on 3D spaceship 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