Problems with simple AI script

Hi there I’m planning to put AI into my game so after some research I got something similar to what I want but it’s still very buggy. The AI seems to only want to face the target’s ass, so when I do a 180 turn with my player the AI spins around the player at the same distance. It’s hard to explain without running it yourself but here it is.

using UnityEngine;
using System.Collections;

public class AIScript : MonoBehaviour 
    public GameObject target;
    public float moveSpeed = 20;
    public float rotationSpeed =5;
    private float distance;
    private float finalmove;
    private float finalrot;

    void Start()
        finalmove = Random.Range(1, moveSpeed);
        finalrot = Random.Range(1, rotationSpeed);
        target = GameObject.FindGameObjectWithTag("Player");

	void Update () 
        distance = Vector3.Distance(target.transform.position, transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation,
        Quaternion.LookRotation(target.transform.position - transform.position), finalrot * Time.deltaTime);
        if (distance > 1)
            transform.position += transform.forward * finalmove * Time.deltaTime;

Any help would be appreciated, even if it involves writing a new script

Thanks in advance

The AI can only turn while going forward (like a car) and can’t turn on the spot?

Not sure if I get you right but slerping the rotation with a given rotation/move speed ratio could result in a large turn radius, then it will never reach the target in some cases.

You could tweek the move speed and rotation speed values or set the rotation and position independently.

Thanks for all the help everyone I eventually fixed it by using the script :

        transform.position += transform.forward * moveSpeed * Time.deltaTime;

man this is so amazing we dont even need a nav mesh agent cool man

i support this