- Home /
NPC LookAt smoothing
Hello there. I've been trying to make a script that allows NPC to look at the character when they are in the trigger zone. So far I have been able to make them do so, but whenever the character goes out of the zone, the NPC snaps back to the initial position immediately. How do I smooth out the movement?
This is the script I'm using.
var targetObj: Transform;
var head: Transform;
var point: Vector3;
var initialhead: Quaternion = Quaternion.identity;
var speed = 0.1;
var resethead: boolean;
var last: Quaternion;
function OnTriggerStay() {
point = targetObj.position;
head.transform.LookAt(point);
}
function Update() {
if (resethead == true);
head.transform.rotation =
Quaternion.Lerp(last.rotation, initialhead.rotation, Time.deltaTime*speed);
}
function OnTriggerExit() {
head.transform.rotation = last;
resethead = true;
}
Thanks in advance.
there are several things wrong with this script. First thing Quaternion does not have variable for rotation. So the values in your Lerp won't work. Next you are never setting "last" so your lerp does nothing except set the rotation back to Quaternion.identity. Also in OnTriggerExit you are setting your "head.transform.rotation = last" which is setting it to Quaternion.identity. Adjustments to these issues.
function Update() {
if(resethead) //since resethead is a boolean you don't have to compare it to true if it is true it will continue (not wrong just preference)
{
head.transform.rotation = Quaternion.Lerp(last, initialhead, Time.deltaTime * speed);
}
}
function OnTriggerExit()
{
last = head.transform.rotation;
resethead = true;
}
Answer by crocodile5 · Apr 13, 2012 at 01:17 PM
I use iTween for basic look rotations and turning. Very easy to use to make simple damped turning of objects and some other stuff. Hope it helps you out.
Answer by Yonlop · Apr 13, 2012 at 03:56 PM
Thanks for the answers. I actually scrapped most of the previous script and figured out this script which works. And now I just need to make the LookAt smooth instead.
var targetObj: Transform;
var head: Transform;
var point: Vector3;
var speed = 10;
var target = Quaternion.Euler(0,0,0);
function Update() {
//this part is to detect player in front of object
if (targetObj) {
var forward = transform.TransformDirection(Vector3.forward);
var toOther = targetObj.position - transform.position;
print (Vector3.Dot(forward,toOther) );
if (Vector3.Dot(forward,toOther) > 0.8 && Vector3.Distance(targetObj.position, transform.position) < 2)
head.transform.LookAt(targetObj);
//returns the object to initial rotation else head.transform.rotation = Quaternion.Slerp (head.transform.rotation, target, Time.deltaTime * speed); }
I just started scripting today and I am really trying to figure out most stuff.. and my head is quite overloaded now.
Thanks again for the quick answers!,Thanks for the answers. I actually scrapped most of the previous script and figured out this script which works. And now I just need to make the LookAt smooth instead.
var targetObj: Transform;
var head: Transform;
var point: Vector3;
var speed = 10;
var target = Quaternion.Euler(0,0,0);
function Update() {
if (targetObj) {
var forward = transform.TransformDirection(Vector3.forward);
var toOther = targetObj.position - transform.position;
print (Vector3.Dot(forward,toOther) );
if (Vector3.Dot(forward,toOther) > 0.8 && Vector3.Distance(targetObj.position, transform.position) < 2)
head.transform.LookAt(targetObj);
else head.transform.rotation = Quaternion.Slerp (head.transform.rotation, target, Time.deltaTime * speed);
}
I just started scripting today and I am really trying to figure out most stuff.. and my head is quite overloaded now.
Thanks again for the quick answers!
Your answer
Follow this Question
Related Questions
Lookat offset angle combined with RotateAround. Basically orbit + rot angle 3 Answers
rotate camera to face a specific object? 1 Answer
Problem with Quaternion.LookRotation() 1 Answer
2d game - looking toward velocity for rigidbody player 1 Answer
Merge a rotation(vector3) to a lookAt(quaternion) ? 1 Answer