- Home /
AI Following Problem
I have an AI Zombie Character who should follow me. When he just sees me, he goes to another position( probably my Spawnpoint). So the rotation to my main character is also bugging. I use rigidbody and it makes so many bugs, i just cant understand it. Here is the part of my script :
protected void RotateTo(Vector3 targetPosition) {
Quaternion destRotation;
Vector3 relativePos;
relativePos = targetPosition-transform.position;
Vector3 tap;
Vector3 trp;
tap = targetPosition;
trp= transform.position;
tap.y=0;
trp.y=0;
relativePos = tap-trp;
destRotation=Quaternion.LookRotation(relativePos);
transform.rotation = Quaternion.Slerp(transform.rotation,destRotation,speedRotation*Time.deltaTime);
}
protected void Walk(Vector3 targetPosition){
Vector3 moveDirection = transform.TransformDirection(Vector3.forward);
Vector3 velocity;
Vector3 delta = targetPosition-transform.position;
if(delta.magnitude>minimalDistance){
velocity = moveDirection.normalized * movingSpeed*Time.deltaTime;
// animation.CrossFade("run");
characterAnimationState = AnimateState.run;
}
else {
velocity = Vector3.zero;
// animation.CrossFade("idle");
characterAnimationState = AnimateState.idle;
}
rigidbody.velocity = velocity;
}
Help would be pretty nice :), it should be a little problem.
Answer by robertbu · Feb 24, 2013 at 05:12 PM
When and where are you setting targetPosition? Vector3's are structs, so you'll be using a copy rather than some reference. So unless you are updating targetPosition each frame, the data will become stale. If this is the problem, you might want to change your code to use the Transform of the target and then access transform.position in the code.
targetPosition is setted in the RotateTo and the Walk function Rotate To(Vector3 targetPosition) and Walk(Vector3 targetPosition) hmm so u mean that i should change the Vector3 to transform?