Enemy moves randomly within a area.

As the title says, I want to have a script that will make a enemy randomly move to a point within a area and then move to the next random point in the area and so on. However I don’t really have an idea on how to do this.

I did try to create a script to see if i could do it but it doesn’t work the way i want it sadly but it’s what i expected. This is the code:

void Roaming()
        if (!inRange)
            v3 = Random.insideUnitSphere * 5;
            v3.y = 0.0f;
            transform.position = Vector3.MoveTowards(transform.position, v3, moveSpeed * Time.deltaTime);

            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(v3 - transform.position), moveSpeed * Time.deltaTime);

I can tell someone is going to point out most likely a big thing wrong with the script but can anyone help me with my script or help me create an entire new script to do what I want it to do.

Hey Codelyoko363,

With your problem, there is MULTIPLE ways to go about this.

What I try to do when I do movement, especially random movement is I use the navmeshagent and tell it to do add random positions to the previous position so it looks some what natural and yet still random and not too wacky.

If you require further assistance on this then I am happy to provide further assistance.

Kind Regards,