Homing missiles - Searched the forum but still no love.

I know this topics been covered many times. I’ve read around 50 threads on the subject but I simple can not get my missiles to track properly.

I’m making a bullet-hell style shooter in 3d, but all of my game objects are restricted in the z axis to 0, so it’s really 2d in 3d.

I am trying to implement missiles that track their target without using the apply force and rigid body manipulation.

The quaternion based solution is:

Quaternion rotation = Quaternion.LookRotation(myTransform.position-myTarget.transform.position);
			
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotation, Time.deltaTime * 5f);

Later in the function I call

myTransform.Translate(Vector3.up * amtToMove);

To move the missile.

The supporting code selects a target and draws a debug line to the target so I know my target selection is working fine. The missiles track some wonky path that is unrelated to what I am trying to accomplish.

I’m close to just hacking a vector comparison test and rotating in the proper direction but I’d love to do this the right way instead.

Code would be great but an explanation of which functions to use and why would be greatly appreciated.

**Solved -

The solution was posted by meanstreak in This thread

Many thanks.

It never hurts to post the “classic” method programmers use to make things look at other things:

function Angle2D(x1:float, y1:float, x2:float, y2:float)
{
	return Mathf.Atan2(y2-y1, x2-x1)*Mathf.Rad2Deg;
}

Atan2 has long been the basic trig way of getting an angle between 2 points. In this case what I would do is use Angle2D to specify which direction I should turn in then:

function TweenAngle( p1:float , p2:float , t:float)
{
	var aDif:float = p2-p1;
	
	if (aDif >= 180.0)
	{
		aDif -= 360.0;
	}
	else
	{
		if (aDif <= -180.0)
		{
			aDif += 360.0;
		}
	}
	return p1 + t * aDif;
}

…Turn from angle p1 (current angle) to the destination angle smoothly as defined by Angle2D function above, by smoothness t. Providing Your movement is fairly constant it would give realistic ish homing missiles that curve around and overshoot (depending on how strong t is).

Oh, and if using physics you’ll want to rotate using Unity - Scripting API: Rigidbody.MoveRotation as its physics-friendly.

Often, you’ll find the unity built-in methods are somewhat not what you want. They are designed for very general purposes.