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;
}
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.
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.