- Home /
Make an object rotate to face the player (but only on x and z axes)
So I have a model of a person that I want to constantly rotate to face the player when the player moves. Here is the code:
var direction:Vector3 = ObjectB.transform.position - transform.position;
rotation = Quaternion.LookRotation(direction); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotincrement);
It works but the problem is that the "position" property of the player is actually around head height, so the other person is kinda tilting upwards as it gets closer to the player, eventually resulting in it falling over. Is there any way to do this so that the other person only rotates on 2 axes instead of all 3?
Answer by deltamish · Oct 28, 2012 at 02:24 PM
You are almost there Add this to your script
`transform.eulerAngles = Vector3(0,transform.eulerAngles,0)//Rotate y,not x and z`
Answer by The-Arc-Games · Aug 06, 2012 at 05:20 PM
There's many ways to achieve what you need. The simpler one being:
- create an empty gameobject and name it 'player ground'
- child it to the 'player', and align it with the 'ground' of the player
- use the 'player ground' object as a target for your turret
Ideally though, your 'player' model origin should be at ground level, so that all controller operations are consistent with the floor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075248im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Aligning an object's rotation direction to a vector 2 Answers
How to make an enemy that walks around a platform? 0 Answers
Armature Rotation, lookAt on y axis, not z axis. 0 Answers
Rotating sphere with rigidbody attached 1 Answer
How do I rotate a circle of objects so that a specific object is in front of the camera? 2 Answers