1

I have a missile in my game and I want to keep track of the distance it has traveled to compare against a maximum range.

As it could conceivably travel along a curved path, just comparing its current position against its starting position won't work for me.

I know I can use .magnitude or Vector3.Distance each time through the Update loop, but I also know that's a pretty big performance hit.

I'd appreciate any suggestions.

user3071284
  • 6,955
  • 6
  • 43
  • 57
Bakewell
  • 13
  • 1
  • 1
  • 4
  • Does it have a constant speed? If it does you could manually determine how long it would take to travel the desired distance and then instead destroy the object after the computed time. – Taelsin Nov 25 '15 at 17:35
  • That's a great suggestion, but unfortunately no it doesn't have a constant speed. It accelerates from 0 to its maximum speed over a period of time. – Bakewell Nov 25 '15 at 17:54
  • Luckily there is a formula for that as well. Check this out http://www.dummies.com/how-to/content/how-to-calculate-time-and-distance-from-accelerati.html – Taelsin Nov 25 '15 at 18:05

1 Answers1

0

Thanks Taelsin. For the moment I'm just going to update the distance traveled using magnitude every x number of seconds using Invoke Repeating. It's not exactly performant, but it's simple. Once I get a little more time, I might do what you suggest and perform some simple physics calculations to figure out how long it takes to travel the maximum distance.

Bakewell
  • 13
  • 1
  • 1
  • 4
  • I would not bother for so few. Despite the fact that distance requires computation, it is fairly nothing. If really you want to save on that, you could use sqrMagnitude which eliminates the sqrt. But the value is less intuitive. But honest, at that point you should not bother about it and just run it. Btw, the fact you only print out at interval of time, do you think the waiting happens magically? The InvokeRepeating needs to create a new process, that is perform each frame aside the update and keeps track of current time and elapsed time. So much for saving isn't it? – Everts Dec 02 '15 at 19:58