Getting an objects "distance traveled per update"

Hello, I’m using spritemanager bullets in my game and transforming the gameobject holding the sprite normally - using raycasts to detect collisions ahead of the bullet

However, I’m working out the raycast distance I should be using by trial and error and would like to somehow calculate the distance the bullet will travel over a single update so I can match the raycast to that distance

I have a speedvalue for the bullets and am multiplying it by Time.deltaTime but still don’t really know how to get a workable value I can use for the raycast distance

Could anyone please explain how to do this? :slight_smile:

Well, the bullet’s speed per frame is Time.deltaTime * velocity. If you’re moving it by hand using Transform.Translate, then you can just use the Vector3.magnitude. If you’re moving it by using rigidbody.velocity, then it’s rigidbody.velocity.magnitude * Time.deltaTime.

(Or at least I think it is… I believe rigidbody.velocity is automatically moving based on Time.deltaTime, so the actual movement per frame shouldn’t be the velocity, but it multiplied by Time.deltaTime.)

The main reason for setting the raycast distance is efficiency. Basically, if you set a short distance, then it limits the number of objects the ray could potentially hit and reduces the number of hit tests the engine has to perform. If you just do the raycast to a fixed, short distance, then it won’t have much of a performance hit, but should still register collisions correctly. A distance value of, say, 5 should be fine (unless the bullets are very fast indeed :wink: )

Thanks guys!

I’m translating the gameobject along Vector3.forward * bulletSpeed * Time.deltaTime… Vector3.forward has a magnitude of 1 so I’m guessing I just need to have the raycast length as bulletSpeed * Time.deltaTime :slight_smile:

The only reason I’m wanting to change it is to reduce the chance of it going through objects, I figured if the length of the raycast is the max distance is can move over a single update then it should never go through an object (I may be totally wrong though, as I am a bit of an idiot)