- Home /
My Zombie Model is coming at me sideways with the AI
Ok so I have my AI script and an empty gameobject declared a trigger so when you come in contact with the trigger the enemy comes at you. So the enemy follows me but he is turned sideways and he adjust's sideways even if I move in front of him. I'm thinking the problem is that the game thinks the side is his face and turns it to adjust there. Can someone help me fix this please?
My AI Script Code:
var target : Transform;
var enemy : Transform;
var moveSpeed : float = 1.0;
var inRange : boolean = false;
function OnTriggerEnter() {
inRange = true;
}
function OnTriggerStay() {
inRange = true;
}
function OnTriggerExit() {
inRange = false;
}
function Update() {
if(inRange == true) {
enemy.transform.LookAt(target);
enemy.transform.position = Vector3.Lerp(
enemy.position, target.position,
Time.deltaTime * moveSpeed);
enemy.animation.Play("Take 001");
enemy.animation.Stop("idle");
}
if(inRange == false) {
enemy.animation.Stop("Take 001");
enemy.animation.Play("idle");
}
}
PS Ignore the animations
Answer by aldonaletto · Oct 12, 2012 at 11:58 PM
transform.LookAt points the object's Z axis to the target transform (or position). If the enemy is looking in the wrong direction, it's not facing the Z axis - maybe its front side is in the X direction instead. The simplest way to fix this is to replace the LookAt instruction with something more versatile, like this:
if(inRange == true) {
var dir = target.position - enemy.position; // calculate the target direction
dir.y = 0; // keep the direction strictly horizontal
// rotate enemy's right side to the target
enemy.rotation = Quaternion.FromToRotation(Vector3.right, dir);
enemy.transform.position = Vector3.Lerp(...
This code rotates the enemy right direction to the target. If it's not ok, try Vector3.left instead.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
looking to make for a zombie script AI,navmesh or not? 2 Answers
Random movment, like mobs in wow 1 Answer
ZOMBIE AI SCRIPT 1 Answer
Damage trigger? 1 Answer