Make AI follow me without floating or clipping? (JavaScript)

Currently, I have set up an AI that when the game is running, will follow you around. Unfortunately, if I jump, the AI will float up trying to follow me, then float back down when I fall. Also, If I walk around a wall quickly, the AI will phase through the wall towards me. How do I prevent this? The following is the code for the AI, and is written in Javascript

#pragma strict
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
 
function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}
 
function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player
 
}
 
function Update () {
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
 
    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
 
 
}

It floats up and down and clips into the ground because it has no physics. In order to add physics, attach a Rigidbody component onto the AI GameObject. After you attach the Rigidbody, it is okay to keep the direct position change script, but will be better to convert to the Rigidbody AddForce for more realistic physics, but some people say it is like throwing itself at you.

A simple collider isn’t constrained by other colliders, unless it has a Rigidbody or CharacterController and is moved with the appropriate functions. In your case, the easier solution is to add a CharacterController to the enemy, adjust its dimensions and move it with SimpleMove (SimpleMove applies gravity automatically, and only requires the velocity vector). Your code adapted to a CharacterController would be something like this:

#pragma strict

var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
 
private var myTransform : Transform; //current transform data of this enemy
private var controller : CharacterController;
 
function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
    controller = GetComponent(CharacterController); // cache the CharacterController
}
 
function Start()
{
    target = GameObject.FindWithTag("Player").transform; //target the player
}
 
function Update () 
{
    // find the target direction:
    var dir: Vector3 = target.position - myTransform.position;
    dir.y = 0; // ignore height differences to avoid enemy tilting
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed*Time.deltaTime);
    //move towards the player:
    controller.SimpleMove(myTransform.forward * moveSpeed);
}

Notice that the direction has its Y component zeroed in order to eliminate height differences - this avoids the enemy tilting stupidly when the player jumps or is at a different height.

add a rigidbody and freeze the z and x rotation