modifying the first person controller to be a very simple AI

its been a long time since ive done any programing and im having trouble with this small task

i just want to change the fpc script to walk towards a target object instead of using player input, but i cant figure out how to tell it which way it should be facing, i tried using look at but that just snaps the rotation to the targets direction instantly

what formula do i use to make var A contain the rotation needed to face the target object

i hope i wrote that in a way that makes sense

You subtract the target’s position vector from “your” position vector. That creates a direction vector pointing from this pos to the target. The magnitude of that direction vector is the distance to target.

Then you would use the Quaternion.LookRotation function to create a target rotation, then lerp from the current rotation to the target rotation smoothly.

C#

//how quickly we turn towards the target
float rotationSpeed = 0.5F;

Vector3 dir = target.transform.position - transform.position;

Quaternion targetRotation = Quaternion.LookRotation(dir.normalized);
float str = Mathf.Min(rotationSpeed * Time.deltaTime, 1);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, str);

I don’t know if you’d have to do anything else to get the FPC to move, but this is how you figure out how to move.

-Jeremy

thanks now i understand how it works, but im having trouble writing it in javascript so i can put it into the walker code

you might want to take a look at some of the AI in the FPS tutorial. ( I think its the third one)

http://unity3d.com/support/documentation/tutorials/

Also I believe somewhere there is a car racing tutorial that shows you how to make waypoints and move components to them

http://unity3d.com/support/documentation/

Is that what you’re looking for, or something else?

ahhh thanks, the sentry gun script in the fps tutorialhad exactly what i was looking for

one more thing

var target : Transform;
var turnspeed=5.0;



function Update () {
	


var targetRotation = Quaternion.LookRotation (target.position- transform.position,Vector3.up); 
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,Time.deltaTime * turnspeed); 

}

this my code now and it works fine, but how can i get it to face only the x axis position of the target

The Quaternion.LookRotation takes the selected up vector as the second parameter, which is currently positive Y axis (0, 1, 0, or Vector3.up). You should be able to just change what direction it uses for up. I haven’t tried it myself though, never needed to.

One thing that isn’t clear, do you want your object’s x rotation to match the target’s x rotation, or do you want you object to face in the direction to your other object, but pointing a different axis towards it? If it’s the latter, try what I said above, otherwise just modify the object’s x rotation to match the target’s.

HTH,
-Jeremy

i want it to face the target object with the z axis, and ignore the targets height, so the object stays level even if the target is 100 feet up

i have a solution that works for now though