Script isnt moving properly,How to fix my Ai using raycasting?

i have simple random raycasting movement script for the enemy no the enemy isnt suppose to attack me just wonder . The problem is that the enemy just spins in place my enemy for now has been a capsule because i still testing everything and stuff i am using a charcter controller and no rigidbody it just messed it all trust me this a very basic script so if can help I’ll be happy I still new to C# if anyone has a better script or soloution please help .
(mind bad spelling this code got me tired)
,So i have a simple random movement scripts here but it seems to have failed me the scripts allows the "enemy " to move around randomly but instead it just spins in circle and i can’t do anything to fix adding a rigidbody makes it worse and yes i am using a character controller and nothing i have with and character controller please help if you can here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WanderingAI : MonoBehaviour
{
public float speed = 3.0f;
public float obstacleRange = 5.0f;

void Start()
{
    
}

// Update is called once per frame
void Update()
{
    transform.Translate(0, 0, speed * Time.deltaTime);

    Ray ray = new Ray(transform.position, transform.forward);
    RaycastHit hit;
    if(Physics.SphereCast(ray, 0.75f, out hit))
    {
        if (hit.distance < obstacleRange);
        {
            float angle = Random.Range(-180, 180);
            transform.Rotate(0, angle, 0);
        }
    }
}

}

The reason your enemy is spinning in place is likely because your spherecast is colliding with the enemy unit itself. There are a few ways to fix this -
**

  1. You could put your enemy on a seperate layer from the obstacles in your scene, then tell your spherecast to use a layermask that ignores the enemies layer. However enemies will not be able to avoid eachother
  2. If you do want enemies to avoid eachother, you could use SphereCastAll and loop through all colliders hit and check if they are valid colliders to avoid
    **
    I would also make sure that your spherecast isn’t colliding with the ground (using layers is a good way to do this)