When thinking about ways to increase the challenge to your game or how to give your Player and advantage, Heat- Seeking Missiles are often near the top of the list of ideas. But how do you go about making them in system that doesn’t have ‘heat’.
For this, We are going to make a new class called Missile. For some basic tracking, we will want these two variables. One to hold the target we are tracking and the other to serve as a check.
Are Missile’s Movement() is fairly simple. It is coded to travel along it’s forward vector until it passes outside the set bounds in the second half of the method. The tracking is done by the first two lines of the method. “If you are tracking something, Look At it and turn your ‘forward’ towards it.”
transform.LookAt rotates the object it is attached to so that it’s Forward vector is pointing towards the assigned Transform or Vector. By default, It then adjusts the object’s Up vector towards Y. Since our game is using the X/Z plane, this works naturally for us. If you are Using the X/Y, you will have to add a second parameter to LookAt. Likely, Vector3.forward or Vector3.back depending on which direction your camera is from the plane.
Now, We have to set are Missile’s target. For this we use a public method, SetTarget(). This way we can let the Class that instantiates the Missile to set the Target and then let the Missile take over.
And of course, We will need the opposite Method in case the Target we are tracking gets destroyed by other means. We want to do this because we are using a Pool system and the Enemy that just got destroyed could be the next one to be spawned in.
Now we have the basics for a Tracking Missile that both the Player and the Enemies can use.