Enemy AI Chase Script c#

In the game I am working on I have red bees and yellow bees.

The red bees are supposed to look at the player when the player comes within a distance of 10 units and then follow the player until the player is able to escape by getting 5 units away, at which point it will stop chasing the player.

My script works, except that the bee aims the stinger at the player (which I will use for an attack script later perhaps), but right now I want the bee to face the player. And when it stops chasing the player it does not turn itself back into its original position. What am I doing wrong?

(By the by, SUPER new to coding. I have very little idea of what I am doing. I’ve been trying to piece together snippets I learn on YouTube)

using UnityEngine;
using System.Collections;

public class chase : MonoBehaviour
{
    public Transform player;

    void Start()
    {

    }

    void Update()
    {

        if (Vector3.Distance(player.position, this.transform.position) < 10) //if player comes within a distance less than 10, Apex activates
        {
            Vector3 direction = player.position - this.transform.position;
        //right now this causes Apex to face the player with its stinger. this will be useful for the virus effected, but wasn't what I was going for

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                            Quaternion.LookRotation(direction), 0.1f); //same as above note. not sure what went wrong

    if(direction.magnitude > 5)
    {
        this.transform.Translate(0,0,0.05f); //this is supposed to stop the chasing and make Apex sit right side up again if the player moves far enough away. so far it just stops chasing
    }

        }

    }

}

Here you can see what is happening. He turns onto his side when he should be rotating to face me, and he moves SUPER slow.

Ideally I want the script to add a public float that allows me to choose how quickly each individual game object with this script moves.

If it matters, he is facing on the X axis. (I don’t know why my image won’t show)

Any and all help is appreciated.

You probably want to put an extra GameObject in at the top of the entire bee prefab so that you can move it and pivot it independently, and be able to play a sting animation without changing the direction of the higher-up game objects.

Like so in your hierarchy:

GameObjectPivot
OriginalBeePrefab

That way your script can drive each transform (the pivot and the bee) separately according to what you want to happen.