x


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.

more ▼

asked Apr 13 '12 at 10:05 AM

Yonlop gravatar image

Yonlop
0 3 3 3

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;
}
Apr 13 '12 at 12:52 PM Adamcbrz
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

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.

more ▼

answered Apr 13 '12 at 01:17 PM

crocodile5 gravatar image

crocodile5
51 4 8 10

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Apr 13 '12 at 03:56 PM

Yonlop gravatar image

Yonlop
0 3 3 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x459
x338
x80
x30

asked: Apr 13 '12 at 10:05 AM

Seen: 1143 times

Last Updated: Apr 13 '12 at 03:56 PM