- Home /
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);
}
}
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.
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.
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
Follow this Question
Related Questions
Calculate the distance a object has moved. 3 Answers
Moving object a certain distance at certain speed 1 Answer
Accelerate/Decelerate Custom Game Clock 2 Answers
How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller 0 Answers
Increase variable for a few seconds 2 Answers