AI going crazy and dont know why

i have a zombie with two movement controls, a wander and a follow. i have them separated by a script to flip when a certain distance is reached. the first transition works and the other

here is the three codes

Switcher

#pragma strict

var EnemyDist : float = PlayerCasting.DistanceFromZombie;
private var com1 : wander;
private var com2 : huu;

function Start ()
{

    com1 = GetComponent(wander);
    com2 = GetComponent(huu);

}



function Update () {

    EnemyDist = PlayerCasting.DistanceFromZombie;
    if (EnemyDist > 12)
    {
    com1.enabled = true;

    com2.enabled = false;

    }
    else if (EnemyDist <= 12)
    {
    com1.enabled = false;

    com2.enabled = true;
    }
}

Wander

#pragma strict
/// <summary>
/// Creates wandering behaviour for a CharacterController.
/// </summary>
@script RequireComponent(CharacterController)
    var speed:float = 5;
    var directionChangeInterval:float = 1;
    var maxHeadingChange:float = 30;
    var heading: float=0;
    var targetRotation: Vector3 ;
    function Awake (){
        // Set random initial rotation
        transform.eulerAngles = Vector3(0, Random.Range(0,360), 0);  // look in a random direction at start of frame.
        //StartCoroutine
        NewHeadingRoutine ();
    }
    function Update (){
        var controller : CharacterController = GetComponent(CharacterController);
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, targetRotation, Time.deltaTime * directionChangeInterval);
        var forward = transform.TransformDirection(Vector3.forward);
        controller.SimpleMove(forward * speed);
    }
    /// <summary>
    /// Repeatedly calculates a new direction to move towards.
    /// Use this instead of MonoBehaviour.InvokeRepeating so that the interval can be changed at runtime.
    /// </summary>
    while (true){
        NewHeadingRoutine();
        yield WaitForSeconds(directionChangeInterval);
    }
    /// <summary>
    /// Calculates a new direction to move towards.
    /// </summary>
    function NewHeadingRoutine (){
        var floor = Mathf.Clamp(heading - maxHeadingChange, 0, 360);
        var ceil  = Mathf.Clamp(heading + maxHeadingChange, 0, 360);
        heading = Random.Range(floor, ceil);
        targetRotation = new Vector3(0, heading, 0);
    }

follow

var target : Transform; //the enemy's target
var moveSpeed : float = 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 () {
var controller : CharacterController = GetComponent(CharacterController);
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

var forward = transform.TransformDirection(Vector3.forward);
controller.SimpleMove(forward * moveSpeed);


}

idk what is happening

As soon as I saw the way he moved, I instantly thought, “Is he using Euler angles?” Sure enough, you are!

Euler angles suck. They’re awful. Don’t use them. I can almost guarantee that’s what’s causing your issue.

Make your targetRotation a Quaternion, and use Quaternion.Slerp.

Heh heh. That’s one funky zombie.

Yes, you could work entirely in quaternions… or, since your rotation is essentially 2D anyway, just keep your angle as a single float (the rotation in degrees around the Y axis). Use Mathf.LerpAngle or Mathf.MoveTowardsAngle as needed. And then simply assign transform.eulerAngles = new Vector3(0, rotation, 0) on each frame. (This is one of the few safe and valid uses of EulerAngles.)