- Home /
Look At Player with Clamping.
Okay, I think I might be a little tired, cause I cannot even begin to work this out. I am almost positive there is a simple solution to this, but I cannot think of what it is.
In my current project, I have an Enemy that always look towards the player with a raycast. as closely as it can. It should however only be able to turn a max of 45 degrees from directly forward.
I think it could be done by casting a ray a long the objects forward vector while rotating the object itself back and forth. How would I get the object to turn as close as it can to the player without rotating beyond 45 degrees in either direction of forward
Answer by Llama_w_2Ls · Feb 01, 2021 at 08:47 AM
You can clamp the euler angle rotation of the enemy game object. Can't you?
// Rotate towards player
enemy.transform.LookAt(Player.transform);
// Make sure enemy's rotation is clamped between -45 and 45 degrees
enemy.transform.eulerAngles = new Vector3
(
enemy.transform.eulerAngles.x,
Mathf.Clamp(enemy.transform.eulerAngles.y, -45, 45),
enemy.transform.eulerAngles.z
);
I actually don't think this might work, since euler angles are clamped between 0 and 180. Let me know, and I'll try to use quaternion rotation instead.