• 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 Cherno · Oct 18, 2013 at 11:11 AM · timespeeddistancebulletimpact

Calculating a bullet time of impact based on speed and distance

TL;DR:

Time = Distance / Speed

but with Unity3D's time and distance units, it not as easy as with, say Miles as distance and MPH aas speed.

How does one calculate Time of Impact based on Distance and Speed?

EDIT: Multiplying the Speed by 50 seems to do the job though.


I am currently scripting projectile behavior. I instantiate a bullet prefab when a weapon is fired, and the bullet has it's own script and is given some information like how fast it should travel, what it's target is, if it should hit the target, damage, and so on.

It is for a turn-based RPG, so there will seldom be more than 10 bullets at once in the scene.

If the bullet reaches the target's position, it should damage if it and also be destroyed. Otherwise, it should travel a until it's "life timer" runs out, and then be destroyed (simulating stray shots).

I hafve tried to do it via collision, but it was too unreliable, the bullet didn't always register the target's collider and so on.

Now I am opting for a more reliable approach: Just tell the bullet when it would reach the target based on the distance to target, bullet speed. If the time threshold is reached, it knows it is at the target's position and should cause damage etc.

EDIT: Code Snippet:

 var t : float;
 var Speed : float = 0.5;
 var Target : GameObject;
 var Move : boolean = true;
 
 var ImpactTime : float;
 ImpactTime = Vector3.Distance(transform.position, Target.transform.position) / (Speed * 50);
 
 function FixedUpdate()
 {
     if(Move == true)
     {
         transform.Translate( Vector3.forward * Speed, Space.Self );
     }
     
     t += Time.deltaTime;
     
     if(t > ImpactTime)
     {
         //Deal damage to target, not relevatn for this example
         Destroy (gameObject);
     }
     
 }
 
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
1
Best Answer

Answer by robertbu · Oct 18, 2013 at 04:33 PM

If you are multiplying the speed by 50, then you probably have an issue with your time calculation. Post the code and we can take a look. The issue of fast moving objects not registering a collision is a well know problem with several partial fixes. It is possible you can address this issue and use the collision. The top three are:

  • Decreasing the Fixed Timestep from the default .02 to .01 or below

  • Use the DontGoThroughThings Wiki script

  • Use Raycasts instead

As an alternate for your approach, since your bullet knows the target, why not check the distance to the target each frame. If it falls below some threshold, then you have a hit. If the distance starts to grow, you have a miss.

Comment
Add comment · Show 4 · 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 Cherno · Oct 18, 2013 at 04:47 PM 0
Share

Thanks, I edits my OP and added the condensed code.

avatar image robertbu · Oct 18, 2013 at 06:19 PM 1
Share

Your speed issue is clear. Dividing by 50 works because it is the same as multiplying by Time.deltaFixedTime which is 0.02 by default. One way to make this all clearer is to:

1) Set your speed to '25'.

2) Change your Translate to:

 transform.Translate( Vector3.forward * Speed * Time.fixedDeltaTime, Space.Self );

3) Remove the ' * 50' from your Impact time calculation.

With these changes, your 'speed' now represents real world units of travel per second.

avatar image Cherno · Oct 19, 2013 at 04:53 PM 0
Share

It works! Thanks!

avatar image Cherno · Oct 21, 2013 at 07:03 PM 0
Share

BTW, thanks for the suggestion about checking the distance to the target. I didn'T even thought of that even though it's so simple. Thanks again.

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

14 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

Related Questions

Destroy instances after set time 1 Answer

Reset speed after a specified time 0 Answers

How do I make this script emit different particles for different surfaces & objects? 2 Answers

How to add impact sparks / blood effects to 2D shooter? 2 Answers

Creating an equation to simulate time based on rate and distance. 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