- Home /
Make GameObject rotate forward relative to other gameobject
I want Enemy rotate straight to Player. So, A Enemy Fire to Player
Answer by ignacevau · Dec 25, 2018 at 09:41 AM
This is a common situation and I believe that if you had done some research, you would've figured it out yourself. Anyways, supposing that you want a smooth rotation, I recommend using Quaternion.Slerp() instead of LookAt(). A way to do this would be:
public Transform Player; private void Update() { Quaternion newRotation = Quaternion.LookRotation(transform.position - Player.position); newRotation.z = Player.transform.rotation.z; newRotation.x = Player.transform.rotation.x; transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 8); }
Since you did not tell, I assumed that you would like to rotate the enemy on the y-axis (note that the rotation on the z and x axis remain unchanged). This would work for both a 2D and 3D environment.
Your answer
Follow this Question
Related Questions
decimal to fraction (just display) 1 Answer
Find center point of regular polygon 1 Answer
Couping Navmesh and animations 0 Answers
Health - Inverse GUI Health Bar 1 Answer
Need help for direction calculation. 1 Answer