In my current project i use unity navmesh for ai navigation.i want my npc’s be able to get a random walkable destination(point) on navmesh and flee to that point when he/she see the player.
Any help or tips on how to do it, I’d be grateful.
Hello guys,maybe im too late,but i just want to say,im soo lost,how can i use this script,please put full script?!?!? I dont understand those lines,please put full script with functions and everything,these lines just makes my head hurt. i need to ai flee away from me,but these lines are soo incomprehensible ,PLEASE just finish this f script. Thanks :)
@Marks981 what don't you understand? If there's any specific point you're not sure about, read the docs about it. If that didn't help, try reading what others say about it. If that didn't help, feel free to ask about it :)
Hello bigbat, I just wrote the code, hope it will help. InvokeRepeating("GameObject_ChangePosition", 0.0f, 1.0f); void GameObject__ChangePosition() { GameObject_targetPoint = new Vector3(Random.Range(-8.0F, 8.0F), 0, Random.Range(-4.5F, 4.5F)); } void Update () { if(GameObject.GetComponent<NavMeshAgent>() != null) GameObject.SetDestination(GameObject_targetPoint); }
Beware that this may make an agent that's near the edge of the navmesh run to the origin! (If radius is > 1 and it picks a point more than 1 away from the navmesh, it returns Vector3.zero.) Better to return transform.position or a Vector3? so the caller knows when it failed (they'd probably want to retry).
It works perfectly and my agents can move freely in scene,of course i modified it a few to fit with my script. thanks a lot.
– bigbatsorry to bump an old thread but what functions do you suggest i use theese in
– PvTGregHello guys,maybe im too late,but i just want to say,im soo lost,how can i use this script,please put full script?!?!? I dont understand those lines,please put full script with functions and everything,these lines just makes my head hurt. i need to ai flee away from me,but these lines are soo incomprehensible ,PLEASE just finish this f script. Thanks :)
– Marks981@Marks981 what don't you understand? If there's any specific point you're not sure about, read the docs about it. If that didn't help, try reading what others say about it. If that didn't help, feel free to ask about it :)
– vexeHello bigbat, I just wrote the code, hope it will help. InvokeRepeating("GameObject_ChangePosition", 0.0f, 1.0f); void GameObject__ChangePosition() { GameObject_targetPoint = new Vector3(Random.Range(-8.0F, 8.0F), 0, Random.Range(-4.5F, 4.5F)); } void Update () {
– SeasiaInfotechindif(GameObject.GetComponent<NavMeshAgent>() != null) GameObject.SetDestination(GameObject_targetPoint);}