Enemy Model Floating in the Air

ok so basically i have no clue why this is happening but if someone could help me out that would be the best thing in the world. so everything else in my code works fine except the fact when the zombie starts to chase me it starts walking on air or floating. so heres my 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;

}

add a rigidbody to the zombie.

Calculate the direction, then zero the y component to make the direction vector completely horizontal. And try freeze rotation X and Z axis in your rigidbody attribute settings. This way zombie don’t fly :slight_smile:

Heres sample js code:

var target : Transform;
var myTransform : Transform;
var moveSpeed = 3;
var rotationSpeed = 3;

function Awake(){
myTransform = transform;
}

function Start(){
target = GameObject.FindWithTag("Player").transform;
}

function Update () {
    var lookDirection = target.position - myTransform.position;
    lookDirection.y = 0;
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(lookDirection), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}

using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

CharacterController controller;
Transform player;
[SerializeField]
float moveSpeed = 4.0f;
[SerializeField]
float gravity =2.0f;
float yvelocity = 0.0f;

// Use this for initialization
void Start () 
{
	GameObject playerGameobject = GameObject.FindGameObjectWithTag ("Player");
	player = playerGameobject.transform;
	controller = GetComponent<CharacterController> ();

}

// Update is called once per frame
void Update () 
{
	Vector3 direction = player.position - transform.position;
	direction.Normalize();
	Vector3 velocity = direction * moveSpeed;

	if (!controller.isGrounded) {
					yvelocity -= gravity;
			}
	velocity.y = yvelocity;
	direction.y = 0;

	transform.rotation = Quaternion.LookRotation (direction);
	controller.Move (velocity * Time.deltaTime);

}

}