x


Another way to follow an object?

Currently I am using

if ( Vector3.Distance( leader.position, transform.position ) >= distance) {
    transform.LookAt(leader);
    controller.SimpleMove(forward * speed );

But I am curious as to another way to get a npc to follow the 'leader'. This is fine in theory, but the npc's z points towards the leader unrealisticly. Is there a way to get the npc to face the player while keeping its z normal?

Right now I am restricting its z bu using

transform.rotation.z = 0;

I don't want to have such a restriction on it though. In the future I want to add functions to allow the npc to rotate to the ground beneath it, and restrictions like that will be a huge problem. I basically need a new way to get it to follow something more naturally.

Thanks for any help

more ▼

asked May 19 '11 at 08:05 PM

Asuperventure gravatar image

Asuperventure
82 4 5 8

Oh, and I am using a character controller, if it helps. no rigidbodies

May 19 '11 at 08:09 PM Asuperventure
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

What I have done is the past is create a Quaternion that points the character in the right direction, then add the other Quaternion that defines the slope.

Edit: Quaternions are actually really simple to use. Basically you use something like Quaternion.LookRotation to make one, then you make another, then just use the * operator to add them.

Quaternion.LookRotation(transform.forward, Vector3.up) * Quaternion.RotateTowards(something)

The docs are pretty good for this.

more ▼

answered May 19 '11 at 08:10 PM

ckfinite gravatar image

ckfinite
1.3k 5 9 24

Thanks for the start off, but I don't why I am getting error messages. I have tried the original like so

Quaternion.LookRotation(transform.forward, Vector3.up) * Quaternion.RotateTowards(leader.position);

and then tried

Quaternion.LookRotation(transform.forward, Vector3.up) * Vector3.RotateTowards(leader.position);

but I get an error message

'RotateTowards' is not a member of 'UnityEngine.Quaternion'

what's up?

May 20 '11 at 03:05 AM Asuperventure

Sorry, that was a lousy example, sine it does not work and is useless. Ally you really need at the moment is Quaternion.LookRotation(leader.position, Vector3.up), to look at the "leader". If you want to add the slope, just change Vector3.up to the direction vector of the slope.

May 20 '11 at 03:15 AM ckfinite

Sorry, that was a lousy example, sine it does not work and is useless. Ally you really need at the moment is Quaternion.LookRotation(leader.position, Vector3.up), to look at the "leader". If you want to add the slope, just change Vector3.up to the direction vector of the slope.

May 20 '11 at 03:15 AM ckfinite

No example is lousy here. I don't even know where to go with quaternions. Anyway, the new example makes the npc walk forward on their world z infinitely for some reason. They don't rotate in the least. How exactly does Quaternion.LookRotation(leader.position, Vector3.up) differ from transform lookAt?

May 20 '11 at 03:33 AM Asuperventure

Sorry, messed up again. You need the direction, not the absolute position. Try Quaternion.LookRotation(leader.position- transform.position, Vector3.up)

May 20 '11 at 03:36 AM ckfinite
(comments are locked)
10|3000 characters needed characters left
function Update () {

transform.rotation.z = 0;



var hit : RaycastHit;
var castPos = Vector3(transform.position.x,transform.position.y-.25,transform.position.z);
if (Physics.Raycast (castPos, -transform.up, hit)) {
    transform.rotation.x = Quaternion.FromToRotation (Vector3.up, hit.normal).x;
}
}

This makes the character extremely buggy and trippy. I think something is screwy in its update function?

more ▼

answered May 20 '11 at 04:19 AM

Asuperventure gravatar image

Asuperventure
82 4 5 8

The reason why it is so weird is that you are only setting the x rotation of the transform, not the entire thing, and Quaternions don't really like that. Change the line transform.rotation.x = Quaternion.FromToRotation (Vector3.up, hit.normal).x; to transform.rotation = Quaternion.FromToRotation (leader.position-transform.position, hit.normal);

May 20 '11 at 01:24 PM ckfinite

:)... You are right sir, I did that to the code. I was messing with it because without it, the y rotation is completely restricted north in the world. Can't turn at all. Do you know how to release the y rotation? I will stop bugging you after this! Thanks a bunch for all your help- you will be getting super up votes from me!

May 20 '11 at 06:43 PM Asuperventure

The Character Controller actually ignores the Y component of the vector input, so it isn't a problem. Anyway, it would not have really worked since Quaternions don't work like Eulers.

Also, did you make all the changes I showed? The Y rotation is controlled by the first parameter, so it might be the problem.

May 20 '11 at 08:30 PM ckfinite

Yes, I took of the .x's from that line of code, and it locked rotation y rotation again. Do you mean castpos is the problem, or the if statement?

May 20 '11 at 09:21 PM Asuperventure

Does your code look like this: transform.rotation = Quaternion.FromToRotation (leader.position-transform.position, hit.normal);

or like this: transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);

If number 2, then make it look like number one. The reason why is the first parameter controls the direction the rotation is facing, so you original code will have it point up only.

May 20 '11 at 11:07 PM ckfinite
(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:

x727
x329
x323
x316

asked: May 19 '11 at 08:05 PM

Seen: 1359 times

Last Updated: May 21 '11 at 01:42 AM