- Home /
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.
Ins$$anonymous$$d of generating a new point to move to (v3) before every time you move your object, generate a new point to move to only when in range of the last point.
Answer by IHackedDeath · Dec 14, 2015 at 03:19 AM
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,
IHackedDeath.
How would I go about using navmeshagent?
As I've never used it before
Your answer
Follow this Question
Related Questions
Random Movement of Enemy within a defined area 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
Inheritance with enemies 1 Answer
Distribute terrain in zones 3 Answers