- Home /
Changing rotation of Quaternion
I have a character moving towards another character, they are both navmeshagents. However when they arrive they don't always look towards eachother. I want one character (the main character) to always turn and look towards the one he is talking to (or is being talked to). What I need to do is retrieve the rotation of one character (npcObj) and the rotation of the player (playerObj), and then do -180.0f on the npcObj's rotation and place this as the playerObj's rotation.
I am very close I believe, but I dont know how to solve it for my problem:
public static void turnToNPC(GameObject npcObj, GameObject playerObj) {
Quaternion npcY = npcObj.transform.rotation.y;
float smoothSpeed = 0.5f;
Quaternion dir = new Quaternion();
dir.y = (npcY - 180.0f);
playerObj.transform.rotation = Quaternion.Lerp(playerObj.transform.rotation, dir, Time.deltaTime * smoothSpeed);
}
You can also use the transform.LookAt function for the main character. Use the transform of the 2nd character as the target of LookAt:
http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
I can't put together a full answer at the moment but there are helpers on the Quaternion class to assist with things like this... take a look at Quaternion.LookRotation and Quaternion.FromToRotation for starters.
Your answer
Follow this Question
Related Questions
Use Lerp to rotate object 2 Answers
How can I use lerp to rotate an object multiple times? 2 Answers
Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers
Transition current camera rotation to 0,0,0 1 Answer
How do I transition smoothly between two lerps/slerps? 1 Answer