# Slerp homing missle that gets more accurate based on distance to target

Here’s what I have so far. I really like the behaiour of the missle taking a “wide arc” to get to it’s target. However, I am currently stumped as to how to have the missle not fall into an “orbit” around the target if the target is close enough to the launcher.

My main issue is I’m trying to use Vector3.Distance value somehow, as I’m trying to increase the damping value to make the missile more accurate based on proximity. But Distance obvious increases as the object is far away, and decreases as it get’s closer.

ex; if the target happens to be beside the launcher, their should be no arc, but if it is further away, then we should see the slerp

So somehow, I need to invert the value given to me by Distance.

Any thoughts?

Here’s the code so far;

``````#pragma strict

var target : Transform;

var damping: 			float = 1.0;
var speed: 				float = 15;
var dist:				float = 0.0;
var altDist:			float = 0.0;

function Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * speed);
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);

//	if (target)
//	{
//		dist = Vector3.Distance(target.position, transform.position);
//		altDist = dist / 100 + 1;
//	}
//
}

function OnTriggerEnter ( other : Collider )
{
if (other.gameObject.tag == "alien")
{
print ("hit");
}
}
``````

(ignore my commented out sections…I was just fidgeting trying different things…)

One solution would be to correct the aim of the missile towards the target if the missile was closer than some specified distance. The closer the missile, the closer to true aim the missile would be at the start. A bit of untested code that would be added to the above:

``````var correctAimDist = 1.0;

function Start() {
var dist = Vector3.distance(target.position, transform.position);
if (dist < correctAimDist) {
var q = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, q, 1.0 - dist / correctAimDist));

}
}
``````

Another solution would be to vary the damping value. If the missile is closer than some specified distance, then the damping value would be increased so the missile would turn faster. The closer the target, the faster the missile turns.