- Home /
Enemy Follow Script Help
How would I change this script so that when the enemy gets lets say 1 metre in front of the player it stops and that if the player jumps the enemy dosent glitch out?
var target : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;
var myTransform : Transform;
function Awake(){
myTransform = transform;
}
function Start(){
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
P.s I didnt write this script all thanks goes to Orangelightning.
That must be ancient code because I really don't remember writing that, but thanks for the credits :D
EDIT: By "glitching out" do you mean that the enemys rotation gets all weird?
P.s I reformatted you code so it is much easier to read now.
You answered a question with this script here http://answers.unity3d.com/questions/33107/enemy-ai-script.html
and yeah when I jump if the enemy is close enough it spins round and sometimes falls over.
Answer by aldonaletto · Nov 20, 2011 at 02:18 PM
You can consider only the horizontal direction - calculate the direction, then zero the y component to make the direction vector completely horizontal:
function Update () { var lookDir = target.position - myTransform.position; lookDir.y = 0; // zero the height difference myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(lookDir), rotationSpeed*Time.deltaTime); myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; }
Thanks a million, man. I've been searching around the Internet for almost three days looking for a solution to this problem. I just wasn't looking up the right search ter$$anonymous$$ology.
$$anonymous$$aybe a good opportunity to try something like this out
Answer by XYZglitch · Aug 16, 2014 at 12:34 PM
,THIS WORKED THANK YOU SO MUCH! Been looking everywhere!
Your answer
Follow this Question
Related Questions
Stay Back! 2 Answers
Shooting Damage Help 1 Answer
Need help with enemy AI 1 Answer
How do I make a enemy follow me 3 Answers
AI enemy scripting help 1 Answer