slowly turning towards target

Hi there,

i asked this question some days ago,
and also got some very good answer …
but after playing around with it some times,
i found out that it isn`t really what i wanted to have.

The question is: How to make a turret aim at a moving object - for example the player - at a CONSTANT turn rate.

I tried out the following:
1.

transform.lookRotation

works … but turns instantly.

I tried out this variant which uses Quaternion.Slerp … it also works, but:
The nearer the turret gets towards the player,
the slower it moves … AND: vice versa.
So it is still not possible for the player to run around a slow moving turret.
(And if i make it too slow, then it takes for ever until it faces the player if the turn distance is smaller …

3.my last idea was the following:

function faceEnemy()
{
	var targetDir = enemy.transform.position - transform.position;
	var yawChange = Vector3.Angle(targetDir, transform.forward);
	//Debug.Log ("FaceEnemy: yawChange:" + yawChange);
	if(yawChange>0)
	{
		if (yawChange > turnspeed)
		yawChange=turnspeed;
	}
	var ideal = (transform.eulerAngles.y + yawChange)%360;
	transform.eulerAngles = Vector3(transform.eulerAngles.x,ideal,transform.eulerAngles.z);
}

this piece of code works partially … if the turret has to turn left to reach the player, it does exactly what it should: Turning around with it`s turnspeed (10) per Frame (using fixedUpdate)…
but if the player is only one degree to the right,
then it turns around 359 degrees rather tha turning that on degree in the other direction.

Reason is that Vector3.angle only returns an absolute value.
In other programming languages, it usually returns a positive OR negative value so i can see if the turret should turn left or right.
So can anyone answer me what i could do to find out that?

(My problem is perhaps my understanding of higher maths)

Thanks in advance,
David

Why not do this:
Create a child Object that has the same center as the Turret, and assign it to a var in the turrets script (name’s fastEye here).

Then, something like this:

var fastEye : transform;
var speed : float = 10.0;

function Update()
{
 fastExe.LookAt(player); //wherever you get your player transform from

 var angle = Quaternion.Angle(transform.rotation, fastEye.rotation);
 if(angle > 0)
  transform.rotation =
Quaternion.Lerp(transform.rotation, fastEye.rotation, Time.deltaTime * speed / angle);
}

This should basicly work, maybe I did a mistake at the last parameter of Lerp(), I didn’t test that.

If you goto YouTube and watch the Tornado Twins tutorials, they teach you how to do that exact thing.

hi,

i watched the turret part of their tutorial,
but as i mentioned, i just don`t want to do it using this lookrotation and slerp.

both is nice done, but both also has it`s disadvantages,
i want to rotate the turret manually,
and therefore, need to know, if the target is to the left side OR to the right side of the turret.

@Flash:
Solutions with lerp/slerp don`t do exactly what i want,
because the rotation speed is not constant there -
if the turret is 180 degrees away, it will turn faster than it will do when the target is

  • let`s say 10 degrees away.

And the helper-object just uses up memory.

but thanks for your hints,
anyway, does anybody here has an idea how to find out if the object is to the left or to the right ??

thanks,
david

public class Turret : MonoBehaviour
{
	public float m_TurnSpeed = 1.0f;
	public Transform m_Target = null;
	
	void Update ()
	{
		if (m_Target == null)
		{
			return;
		}
		
		Vector3 targetLookAtPoint = m_Target.position - transform.position;
		
		targetLookAtPoint = new Vector3 (targetLookAtPoint.x, transform.position.y, targetLookAtPoint.z);
			// Remove this line if your turret is supposed to be able to look up and down
		targetLookAtPoint.Normalize ();
		
		targetLookAtPoint = Vector3.Slerp (transform.forward, targetLookAtPoint, Time.deltaTime * m_TurnSpeed);

		targetLookAtPoint += transform.position;
		transform.LookAt (targetLookAtPoint);
	}
}

That’s why I divide the 3rd parameter by “angle” :wink:

What system are you developing for that you can’t afford an extra empty object oO