- Home /
hoe to rotate a AI as a animation
hello,
I have a waypoint system if the player reaches the waypoint he wil rotate to the next now i use transform.LookAt
The negative thing about this one is that the AI rotates immidiatly to the waypoint. I want that the player rotates as an animation I use JavaScript. And i dont want to use ITween. tank you
Answer by HarshadK · Oct 17, 2014 at 11:29 AM
Use this:
var speed: float = 0.5;
function Update()
{
var oldRot : Quaternion = transform.rotation;
transform.LookAt (target);
var newRot : Quaternion = transform.rotation;
transform.rotation = Quaternion.Slerp (oldRot, newRot, speed * Time.deltaTime);
}
You can set the value of speed variable to change the speed of rotation.
If i do this the AI wil be stuck on the waypoint i have 3 waypoints and the AI is stuck on the first one.
The code above was to give you an overall idea. You need to put some condition in the Update to make sure the rotation only happens when required.
var speed: float = 0.5;
var shouldRotate : boolean = false;
var isRotating : boolean = false;
var newRot : Quaternion;
function Update()
{
if(shouldRotate)
{
transform.LookAt (target);
newRot = transform.rotation;
isRotating = true;
shouldRotate = false;
}
if(isRotating)
{
var oldRot : Quaternion = transform.rotation;
if(!$$anonymous$$athf.Approximately(oldRot, newRot))
{
transform.rotation = Quaternion.Slerp (oldRot, newRot, speed * Time.deltaTime);
} else
{
isRotating = false;
}
}
}
Set your shouldRotate to true when you want your game object to rotate.
Your answer
Follow this Question
Related Questions
LookAt Problem 1 Answer
How to prevent Z axis rotation ? 1 Answer
LookAt transform plus a y value 1 Answer
making one object a parent of another via javascript 2 Answers
How to prevent Z axis rotation ? 0 Answers