- Home /
Enemy is not stopping following the player
This is my code:
var target : Transform;
var moveSpeed = 0;
var rotationSpeed = 3;
var player : Transform;
var distance = 4;
var distanceAttack = 1;
var myTransform : Transform;
function Awake() {
myTransform = transform;
}
function Start() {
animation.Play("idle");
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if(Vector3.Distance(transform.position, player.position) < distance){
moveSpeed = 2;
animation.Play("walk");
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward moveSpeed Time.deltaTime;
if(Vector3.Distance(transform.position, player.position) < distanceAttack){
moveSpeed = 0;
animation.Play("attack");
myTransform.position += myTransform.forward moveSpeed Time.deltaTime;
}
}else{
moveSpeed = 0;
animation.CrossFade("idle");
}
}
Why ? Can someone help me ?
Answer by deltamish · Nov 17, 2012 at 04:35 PM
hi I have edited your script a bit try using it
var target : Transform;
var moveSpeed = 0;
var rotationSpeed = 3;
var player : Transform;
var distance:flaot = 5.8;
var distanceAttack:float = 2.6;
var myTransform : Transform;
function Awake() {
myTransform = transform;
}
function Start() {
animation.Play("idle");
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
if(Vector3.Distance(transform.position, player.position) < distance){
if(Vector3.Distance(transform.position, player.position) >distanceAttack){
moveSpeed = 2;
animation.Play("walk");
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(Vector3.Distance(transform.position, player.position) < distanceAttack){
moveSpeed = 0;
animation.Play("attack");
}
}
else{
moveSpeed = 0;
animation.CrossFade("idle");
}
}
Answer by programmrzinc · Nov 15, 2012 at 12:10 AM
The attack distance may be too small. I once had a problem just like that, and the attack distance was my problem. Try making your distance larger, and comment about what happens.