- Home /
How do I limit the angle of transform.lookat function
I have searched on google and the unity answers site and still haven't found a solution that works yet. I would like the head of my enemy character to only move -+90 degrees. So like a normal person. Now it just rotates 360 degrees and it is not natural. Is there any simple and safe way to do this. This is what I have already:
public Transform head;
//.... underneath is in an update function
Vector3 playerPos = new Vector3(player.position.x, head.position.y, player.position.z);
head.LookAt(playerPos);
If anyone could help that would be great thank you
Answer by dischargee · Oct 22, 2017 at 11:14 AM
@rodude123 Use: Vector3.Angle(A, B), where A and B are Vector3
//3D if(Vector3.Angle(A, B) >= -90 && Vector3.Angle(A, B) <= 90) { // code for head turning }
// 2D float angle = Mathf.DeltaAngle(Mathf.Atan2(source.y, source.x) * Mathf.Rad2Deg,Mathf.Atan2(target.y, target.x) * Mathf.Rad2Deg);
Code is not tested I wrote it now, but you get the point :D
@dischargee Would I use Vector3.left
and Vector3.forward
for A and B for 90 degrees and Vector3.forward
and Vector3.right
for A and B for -90 degrees
Your answer
![](https://koobas.hobune.stream/wayback/20220612141605im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[C#]CharacterController Turning 2 Answers
Sidescroller Turning 1 Answer
How to get the enemy jumping all around? 0 Answers
Flickery Turning on third person character. 1 Answer
Smoother Turning (New guy here, hi) 1 Answer