- Home /
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
Oh, and I am using a character controller, if it helps. no rigidbodies
Answer by ckfinite · May 19, 2011 at 08:10 PM
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.
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?
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.
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.
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?
Sorry, messed up again. You need the direction, not the absolute position. Try Quaternion.LookRotation(leader.position- transform.position, Vector3.up)
Answer by Asuperventure · May 20, 2011 at 04:19 AM
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?
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);
:)... 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!
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.
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?
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.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Moving while facing the camera 0 Answers
Moving a model relative to one axis 1 Answer
Rotation in 8 way direction 2 Answers
How can I rotate towards/look at a specific axis of a collided object? 2 Answers