- Home /
My zombie rotates sideways when it walks towards me or when I walk to him
It rotates sideways when I go next to it or when it comes to attack me? Heres the code
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
var isNotDead : boolean = true;
var health : float = 100;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
if(health < 1){
isNotDead = false;
animation.Play("die");
Destroy(gameObject, 1);
}
if(isNotDead){
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
var distance = Vector3.Distance(target.position, myTransform.position);
if (distance < 3.0f) {
animation.Play("attack1");
}
else{
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
animation.Play("walk1");
}
}
}
function ApplyDamage(dmg : float){
health -= dmg;
}
Comment
Typically this happens because when the rotation of the character is set to (0,0,0), the character is not facing positive 'z'. The best solution is to fix the mesh in a 3D program. Sometimes you can fix it by using an empty game object as a parent and put scripts like above on the empty game object. Fixing it in code is possible, but usually ugly.
Your answer
Follow this Question
Related Questions
Way Point Script Change Group 0 Answers
Rotating Around Joints 0 Answers
Ai Zombie Melee Attack script. 5 Answers
Camera rotation around player while following. 6 Answers
Follow Player Via Waypoints 1 Answer