Enemy lookAt Player

Hey Guys! I have a problem.

i animated a character, and made a simple AI Script, that the enemy first stares at me, and than (when i am near him) he runs at me. The Problem is: he walks sideways. I looked for answers from others who have the same Problem as me, but it didn’t helped. I tried to rotate the character in Cinema4D to look in front of the camera, but this doesn’t seem to affect the object. Transforming the object in the Inspector doesn’t help too. He always runs and looks at the wrong direction. Here’s the code i used:

var speed = 5.0f;
    var focusDist : float = 10.0f;
     
    var player : Transform;
     
    private var enemyToPlayer : Vector3;
     
    function Update()
    {
    zombiToPlayer = player.transform.position - transform.position;
    var dist = zombiToPlayer.magnitude;
     
    if( dist <= focusDist )
    RunTowardPlayer();
    else
    Idle();
    }
     
    function RunTowardPlayer()
    {
    transform.rotation = Quaternion.LookRotation(enemyToPlayer);
     
    gameObject.GetComponent( CharacterController ).Move( enemyToPlayer.normalized * speed * Time.deltaTime );
    animation.CrossFade( "WFront", 0.3 );
    }
     
    function Idle()
    {
    animation.CrossFade( "Idle", 0.3 );
    }

Please help me!

In Update(), add

transform.LookAt(player);

Note that the transform property isn’t a variable. It actually calls GetComponent() every time you reference it. If you need to optimize Update(), consider storing the value of transform in a variable once in Start() and using that variable instead.

As you enhance this, you’ll probably want to use Mathf.SmoothDamp() to gradually rotate toward the player instead of Look(), which changes direction immediately. But LookAt() will get you started.