The question is answered, right answer was accepted
Stop before player
Hello, I am making a script for an enemy to follow you and I am wondering how would I add it in so the enemy stops lets say 1 before you and not just continue to circle me? Thank you in advanced!
Here is my script:
var target : Transform;
var moveSpeed = 20;
var rotationSpeed = 5;
var myTransform : Transform;
private var realRotation : Quaternion;
function Awake() {
myTransform = transform;
realRotation = myTransform.rotation;
}
function Start() {
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
myTransform.rotation = realRotation; // Resume it
myTransform.rotation = Quaternion.Slerp(
myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position),
rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
realRotation = myTransform.rotation;
myTransform.rotation = Quaternion.Euler(0, myTransform.eulerAngles.y, 0); // Fake it
}
Answer by Cuttlas-U · Oct 25, 2017 at 11:32 AM
hi; u just need an if statement to check the distance;
if ( Vector3.Distance(enemyTransform.pos , Player.Transform.pos ) > 10 )
{
//chase the player;
}
so this code enemy will follow to player untile its 10 point away;
Thanks for the reply mate!
Problem is though when I add it in the script it doesn't work. Is there any chance you could put it into the script as I might be doing something wrong? Thanks in advanced!
hi again ; it should work ! maybe the value 10 is too big and its already in range ; so show me your full script to check and your self go change the value and test again;
It wasn't that. When I would put the script in it would say "$$anonymous$$ Identifier Player" and the same for "enemy"
Follow this Question
Related Questions
How to use NavMeshAgent on unity 5 car 0 Answers
How can an Animator FSM get public variables?,Making a Variable Holder for an FSM 0 Answers
how to search for a random point until the condition is true? 0 Answers
COMPILER Error 0 Answers
How do I make an AI(cube) that follows and rams the player? 0 Answers