Angle between player and rocket targetting player.

I’m stuck with the problem mentioned in the question. I thought it’s very easy, but I do something wrong. So I have a rocket which is following the player (edited from Smooth Follow script).

Now, this rocket is dodgeable by the player for a reason, but as it can’t turn enough quickly to the player, it starts to set an orbit around the player and it goes around the player game object. The reason is pretty understandable, the rocket is too fast and it can’t turn with that speed, it stucks around the player.

I want to make the player’s situation more dangerous by slowing down the rocket, when it’s angle relative to the player is too high. For example, if angle is larger than 30, slow down the rocket to follow the player easier, if it is less than 30 again, fly with maximum speed.

Of course, if the player is behind the rocket (rocket is facing the opposite direction), slow down to the minimum speed allowed for the rocket, find the target and again, fly with maximum speed.

What I was trying:

angleToTarget = Vector3.Angle(target.position,transform.forward);

It’s in Update() of course.

I guess, the problem is that transform.forward, it is applied in world space, not locally. I need a solution that works in local space. I hope it is understandable, I usually over-complicate things, but… nevermind!

A bad drawing about the problem:

Ah you’re nearly there - the problem is that you don’t need to use the target position you need to use the target.position - transform.position to get the vector.

There’s another choice though: you can use the Dot Product of the vectors so:

  var dot = Vector3.Dot((target.position - transform.position).normalized, transform.forward);

This will give you a value between -1 and 1. 1 when the rocket is facing the correct direction, 0 when it is at right angles and -1 when it is facing the wrong way. With a bit of clever clamping and manipulation or perhaps a Lerp you could easily slow down your rocket!

  var rocketSpeed = Mathf.Lerp(minimumRocketSpeed, maximumRocketSpeed, Mathf.Clamp01(dot));