- Home /
Indirect Missile Path
Okay my last question didn't get exactly the answer I was looking for, so I'm going to be more specific. I want to have a projectile that travels towards a given target point that may or not be stationary. The game has a top down view, so the projectile should only travel in one axis.
If I use LookAt to set the forward direction, the projectile is too direct for my tastes. These pictures should illustrate my needs, the box is the object firing the projectile, the green oval is the projectile, the red circle is the target, and the blue line is the path:
Wrong(using lookAt every frame):
Right:
So how can I get results (something) like the second picture? I want to slowly set itself onto the right path, instead of being so direct. I tried the SmoothLookAt script, but it sends the projectile up or down into the Y-Axis, which shouldn't happen in this type of game. It needs to have a constrained altitude like the picture shows.
Answer by Eagle32 · Aug 15, 2010 at 05:52 AM
Another option.
Each update do the following in a script on the bullet/missile:
- Get the position of the target
- Determine the direction to the target from current missile position
- Determine the angle between the direction of the target and the missiles current direction of travel
- Decide how much your missile can turn this update. I like to have a fixed maximum turn rate per second which I multiply by deltaTime to determine how far my object can turn this update.
- Calculate how much of the angle between your current direction and the direction to the target you need to turn through. This will be a value between 0 and 1. 1 meaning you can turn all the way this update. 0 meaning you can't turn any of the distance this update (something's probably wrong if you actually get a 0 value).
- Lerp/Slerp/"some other type of interpolation" between current direction and the direction towards the target. The resulting value is the new direction to move the missile along.
The resulting behaviour is that of a guided missile with a maximum turn rate.
I pretty much ended up doing this. Its flexible, which is pretty important for this game. Thanks.
Answer by CarbonTech Software · Aug 15, 2010 at 06:28 AM
I have an even simpler solution that doesn't require any scripting (unless you want to improve the accuracy somehow). I personally use this technique for my own game. It only requires 3 steps:
1) Attach a rigidbody component to your "missile" - you may want to uncheck the gravity checkbox so that the missile will be more accurate
2) Attach a constant force component to the "missile" object
3) Set the "relative force" of the x axis of the constant force to negative some number (this is the velocity)
When you instantiate the missile (supposedly in the direction of where you are aiming), make sure to set the initial velocity of the object to the current velocity your parent object (which could be a person, car, thing, whatever). That way, it won't whiz behind your player while going at high speeds on instantiation.
To improve the accuracy of this, you would probably have to do some scripting - you could always make another post asking if you are stumped.
I hope this helps.
Answer by Jason_DB · Aug 15, 2010 at 05:50 AM
What you could probably do is every frame send a raycast from the projectile to the target, using it to determine the difference between the projectile's current rotation and the direction to the target. Then change the projectile's rotation to be closer to the target rotation by some set amount (or by a percentage of the difference). Because you're changing the rotation manually you can just not change the y rotation to avoid the problem in SmoothLookAt.
P.S. I'm admittedly kind of late right now, so this could have just all been nonsense.
Answer by Julian-Glenn · Aug 15, 2010 at 02:00 AM
Not sure but what about using iTween and it's LookTo or even Drawpath functions.
Another possible way could be to place some dummy empty gameobjects attached to the target that are offset by a certain amount from the parents transform poition. At first the projectile moves towards the first dummy gameobject, then after a certain time period it switches targets to the second dummy gameobject, etc , etc then finally zeros in on the final target which is the true target. ( just a speculation)