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 aquaphoenix41 · Jul 05, 2019 at 09:16 AM · script.timemathdistancegameobjects

How can I get the Time taken for the 2 moving objects to touch each other

Hello, I'm new in Unity as well as a beginner at Programming and Making Games I want to know How Can I calculate the time taken and make a "Count Down" for the 2 objects moving in the same Direction but with different speed to touch each other.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by AaronBacon · Jul 05, 2019 at 01:41 PM

You could simply make a float variable that's at 0 when the game begins, then add Time.deltaTime to it every frame until it collides with the other object. I would use OnTrigerEnter to test for the collisions, assuming both objects have a RigidBody and a Collider, and theres multiple ways you can check if the object the first object has collided with is the other object or not. One way would be to use Unitys Tag system, or just have the Object store the other object as a variable and check with collidername.gameobject.

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
avatar image
0

Answer by metalted · Jul 05, 2019 at 01:59 PM

So this is more of a physics question then a programming question. So because I don't know the situation I'll assume that there is no acceleration involved. We let the 2 objects move on the same line. Object 1 starts at 100m and has a speed of 10 m/s. Object 2 starts at 0m and has a speed of 20m/s. At what time will object 2 overtake / be at the same position as object 1?


The equation used for this is as follows: Xt = V0t + 1/2at^2 + X0. (Xt = Position after t time, V0 = initial speed, a = acceleration, t = time, X0 = initial position). Using this information we can enter the data we have from the objects to create a position function for both:


Object 1: (X0 = 100m, V0 = 10 m/s, a = 0). This will give us the following: Xt = 10t + 100. The middle part will be 0, because a is 0. For object 2 (X0 = 0m, V0 = 20m/s, a = 0) the equation will be: Xt = 20t. (In this case the middle and last part will be 0, so no need to write it down. Now that we entered our data, the math parts comes:


[Object 1: Xt = 10t + 100][Object 2: Xt = 20t]. We know that because they will be on the same position, that both the Xt have to be the same. This means that both equation have to be the same as well. Now we can solve the equation. If Xt = Xt then 10t + 100 = 20t. Now we use the following simple algebra to solve:

  1. 10t + 100 = 20t.

  2. 100 = 20t - 10t. (Move 10t to the other side, it becomes negative now)

  3. 100 = 10t. (Subtract 10t from 20t)

  4. 10 = t. (Divide both sides by 10).

  5. t = 10. This means that after t=10 or 10 seconds the positions should be the same. Lets check that:


Object 1: 10t + 100 (with t 10). 10 * 10 + 100 = 200 m.


Object 2: 20t (with t 10). 20 * 10 = 200m.


The answers are the same, so we can conclude that we calculated correctly. The objects will meet at 200m.


Now when acceleration is involved, solving this equation is a whole different thing. You will have to use different kind of math. Just so you know.

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

129 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

Related Questions

Increase/Decrease Font Size with distance 2 Answers

Make something arrive at position in exactly X seconds? 2 Answers

Text wont show the distance to the nearest enemy 2 Answers

How to execute a function X times per second. 1 Answer

Increment a variable by using an AnimationCurve graph 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