How do i make my enemy look at player on only one axis?
I am trying to make it so that the enemy looks at the player and I have achieved that but when I jusp it also rotates to the point where it is lying down if it is close up. How do I make it so that it will always stay upright and not "fall over" like it s doing? Here is my current code attacked to the enemy:
void Update() { Quaternion rot = Quaternion.LookRotation (player.transform.position - transform.position); transform.rotation = Quaternion.Slerp (transform.rotation, rot, 1); }
Answer by Arocide · Sep 19, 2017 at 07:25 PM
If you do not wish to worry about the Y axis you can just override the Y value of the calculation with the Y value from the enemy. So for example:
void Update() {
Vector3 lookVector = player.transform.position - transform.position;
lookVector.y = transform.position.y;
Quaternion rot = Quaternion.LookRotation(lookVector);
transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1);
}
This will cause the 'enemy' to only rotate to face the players position on the X and Z planes and ignore the height.
Your answer

Follow this Question
Related Questions
Have enemy constantly try to get in front of player and push them back 0 Answers
How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject 0 Answers
Delayed LookAt, but only on one axis 1 Answer
Using LookAT causes no rotation at all? 1 Answer
How to rotate object in sphere 0 Answers