- Home /
How to create a heat seeker
I am making a 2D tower defense game with unity. I have seen heat seeker type rockets in Kindom Rush that moves around the screen and goes to the target. Really nice effect.
Can anyone give me some direction how to create this type of movement?
Answer by robertbu · Feb 06, 2014 at 05:18 PM
It appears from the brief video glimpses, that the projectile heads straight up, levels off at some arbitrary vector than seeks the target. Here is a bit of sample code. To test start with a new scene. Put this script on a game object with a Rigidbody component, with gravity turned off, and with a 'y' value of zero. In the inspector initialize 'target' to another object in the scene and hit play:
#pragma strict
var target : Transform;
var upHeight = 0.5;
var turnSpeed = 100.0; // Degrees per second
private var initialDirection = Vector3.forward;
private var velocity = 3.0;
function Start() {
initialDirection = Quaternion.AngleAxis(Random.Range(0.0, 360.0), Vector3.up) * initialDirection;
transform.rotation = Quaternion.LookRotation(Vector3.up);
rigidbody.velocity = Vector3.up * velocity;
// Head up for a set distance
while (transform.position.y < upHeight) {
yield;
}
// Level off to an arbitrary vector
var q = Quaternion.LookRotation(initialDirection);
while (transform.rotation != q) {
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * turnSpeed);
rigidbody.velocity = transform.forward * velocity;
yield;
}
// Seek the target
q = Quaternion.LookRotation(target.position - transform.position);
while (transform.rotation != q) {
rigidbody.velocity = transform.forward * velocity;
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, Time.deltaTime * turnSpeed);
q = Quaternion.LookRotation(target.position - transform.position);
yield;
}
}
function OnCollisionEnter() {
Debug.Log("Boom");
Destroy(gameObject);
}
Note this code assumes an object in which the front is facing positive 'z' when the rotation is (0,0,0) (or an object like a sphere that has no explicit front).
Not exactly what i was looking for, but this is really interesting. Thanks
Answer by fafase · Feb 06, 2014 at 12:26 PM
Hey could not really find the rocket you are mentioning in Kingdom Rush. Nonetheless we can try.
So first your rocket has a starting point, either a rocket launcher or outside the screen. Then it has a target point. For now let's consider the target point to be where you clicked.
So we need to move the object from start to target. You can use Translate or Rigidbody, this is up to you.
Now the purpose is to seek for "heat", I would think this is mainly a way to make the difference between a character and the environment. In our case, we will consider that you have a list of potential target, if this is based on heat, then a fire will be on that list, which allows the player to lure the rocket for instance.
You could at a certain frequency, check for the closest item from the list, you could use a sorted list to spare the hassle of ordering. Then, you check if the distance between the rocket and the closest item is small enough to be considered. This range is up to you. If within range, then you change the target for that new target and translate your rocket towards that new target.
At first it will not look good because your rocket will suddenly change direction so you can work it out with some force principle to get the movement in a curve.
fafase thank you for your answer.
It seems I didn't explain what I wanted properly, sorry about that. The problem I am having is how to move the heat seeker rocket around the screen in a looping manner similar to $$anonymous$$ingdom Rush. I am trying to use a brazier curve equation to get similar effect but I couldn't get effect similar to $$anonymous$$indom Rush. Any idea how to do this?
yes that looks great. Here another clip I found. Around 49 he uses similar attack. The attack I have seen looks similar but loops around the screen 3,4 times before going for the target.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132709im_/https://answers.unity.com/themes/thub/images/avi.jpg)