- Home /
How to make AI wonder about? C#?
Hi There!
So, I was wondering how to make a (for example) Monster wonder around until it saw a player. Then Start walking towards you. I have the walk towards part, but I dont know how to make them just walk around!
If you could help that would be great!
Thanks
-Sir
You will probably need some form of a pathfinding algorithm in place if you don't already have one.
For wandering, you don't need pathfinding. A simple steering behavior will do.
I would recommend waypoint ins$$anonymous$$d of pure wandering. Nothing just wanders, it always has a destination, so should your NPC. Wandering agent will probably end up with non-human behaviour like heading towards a zone where there is nothing to do. So waypoint coupled with steering behaviours to avoid obstacles would be better.
I agree with @fafase -- waypoints are a good, too. It really depends on your needs. If you go with waypoints, consider choosing them randomly so it doesn't look like the monster is on a fixed track.
Answer by TonyLi · Apr 09, 2014 at 05:33 PM
Game Development @ Tuts+ has more than you'd ever want to know about wandering:
http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-wander--gamedev-1624
Answer by kyrasharma1092164 · Feb 15, 2021 at 11:07 AM
it is really easy to do so;- 1- make a cube and remove the mesh render component, now create your monster and give it nav mesh agent component, finally create a c# script and set the monster destination to your cube.
code:-
using UnityEnigne.AI;
public NavMeshAjent myAjent; //your monster, make sure it has nav mesh ajent
public Transform Ajent_Destination;// your cube
public Transform temp_playerDestination;// you i mean the player
private bool isChasing_PLayer = false;
void Update()
{
myAjent.SetDestination(Ajent_Destination.position);
if(isChasing_Player == true)
{
myAjent.SetDestination(temp_playerDestination.position);
}
else
{
myAjent.SetDestination(Ajent_Destination.position);
}
}
void OnColliisonEnter(Collider other)
{
isChasing_Player = true;
}
Now we only need to teleport the cube , make sure it has box collider with is trigger set to true make another c# script name it as Collide and ,,,,,
code:-
public int min_X = 0;
public int max_X = 0;
public int min_Z = 0;
public int max_Z = 0;
void OnColliisonEnter(Collider other)
{
this.gameObject.transform.position = New Vector3(Random.Range(min_X , max_X) , 0 /*you can modify this thing by creating min,max variables for Y*/ ,Random.Range(min_Z , min_Z ));
}
And you are good to go, hope this works I haven't tested this script in unity so please just check for spelling cause this surely goanna work , leave the comments for any suggestion or query well this is my first ever unity solution and I am programming for a month now!!!!
sorry for that also make to write OnTriggerEnter instead of OnCollisionEnter
I didn't ask this question, I'm writing my own pathfinding solutions
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
AI in unity, need help 1 Answer
Distribute terrain in zones 3 Answers
[C#]OnTriggerExit set Collided false ??? 1 Answer
Make player not be seen by AI, when player in foilage and shadows. 1 Answer