- Home /
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:
I just read, transform.forward uses local axis... hmmm, then, how should I make it work?
Answer by whydoidoit · Mar 19, 2013 at 06:43 PM
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));
Now, I'm much closer to the solution! Thanks! One thing, that I wasn't able to do yet is the correct speed controlling. I tried to use an AnimationCurve to speed up and slow down my rockets, and the result is not bad, but far from the best.
What I want is if the player dodges the rocket, don't slow down immediately, but slow down slowly, so the rocket doesn't attack the player after the rocket passes next to it. So I want my rocket goes away, turn back and charge at the player. Hopefully, I can do that.
Anyway, is AnimationCurve O$$anonymous$$ in performance?
Sure how about:
float rocketSpeed;
...
rocketSpeed = $$anonymous$$athf.Lerp(rocketSpeed, $$anonymous$$athf.Lerp($$anonymous$$imumRocketSpeed, maximumRocketSpeed, $$anonymous$$athf.Clamp01(dot)), Time.deltaTime);
This will lerp with damping the current rocketSpeed towards the ideal speed based on the current directions. You could also use SmoothDamp.
Thanks, it is working well, just needs a little tweaking, and it'll be perfect!