News › Forums › RAIN › General Discussion and Troubleshooting › How to check point is on the nav mesh?
Tagged: nav mesh crash
This topic contains 22 replies, has 4 voices, and was last updated by abchiptop 1 year, 9 months ago.
-
AuthorPosts
-
February 26, 2022 at 5:03 pm #8565
Yeah - by “crashes” I assume you mean that it becomes unresponsive, which will happen because there is no exit condition for the do while loop. isOnMesh is not really what you want, because it doesn’t tell you if the point is reachable (it could be on a different mesh). The method I suggested does. Of course, even that isn’t
good enough, because no path may exist. There is a method on the Navigator for computing a path manually. That will give you the “does a path exist behavior”. However, if you spawn under a crate - there will never be a valid path.
In any case, you probably want to add additional code to ensure the loop can’t run forever, and to deal with the misc. problem cases (AI not on mesh, target not on mesh, no path exists.)
February 26, 2022 at 5:12 pm #8567Could you suggest what code I could use to test if there is a valid path to a particular point?
February 27, 2022 at 2:51 pm #8640February 27, 2022 at 8:46 pm #8668Could you give some context to that API.
I don’t know how to use the out variable.
February 27, 2022 at 9:07 pm #8679The following code works :
using UnityEngine; using System.Collections; using System.Collections.Generic; using RAIN.Core; using RAIN.Action; using RAIN.Navigation; [RAINAction("Choose Wander Location")] public class ChooseWanderTarget2 : RAINAction { // add in a cube and rename Indicator to display target location //private GameObject Indicator; public ChooseWanderTarget2() { actionName = "ChooseWanderTarget2"; } public override void Start(AI ai) { base.Start(ai); // Use this to assign a cube with collider disabled called indicator to use and an idicator for target position //Indicator = GameObject.Find ("Indicator"); } public override ActionResult Execute(AI ai) { Vector3 loc = Vector3.zero; RAIN.Navigation.Pathfinding.RAINPath myPath = null; do { loc = new Vector3(ai.Kinematic.Position.x + Random.Range(-8f, 8f), ai.Kinematic.Position.y, ai.Kinematic.Position.z + Random.Range(-8f, 8f)); } while ((Vector3.Distance(ai.Kinematic.Position, loc) < 1f) || !ai.Navigator.GetPathTo(loc, 10, out myPath)); ai.WorkingMemory.SetItem<Vector3> ("wanderTarget", loc); // Use this to move the object (ie a cube to the target location //Indicator.transform.position = loc; return ActionResult.SUCCESS; } public override void Stop(AI ai) { base.Stop(ai); } }
This will cause your character to wander about to random locations on the navmesh provided your character starts on the nav mesh.
The behavoir tree should be set up as follows:
BT
-SEQ root
— action
— PAR parallel
— moveThe action should be set up as a Custom Action - repeat never - pause when hit unticked - Assembly (global) - Class: created as the script above pointing to Choose Wander Location (File: ChooseWanderTarget2.cs)
parallel should be set to repeat never, pause when hit unticked - Fail : Any, Succeed : All.
move should be:
Node Type : Move
Name: move
Repeat : Never
Pause When hit: unticked
Move Target : wanderTarget
Move Speed : 1.5
All others left blank.My Character uses Mechanim and so My AI for the character is set to the above Behavoir tree with the Motor set to MecanimMotor and the Param Speed : Speed - my variable used to control speed. ( I have yet to work out the other ones but forward motion looks ok): My Animator is also set to MecanimAnimator although I don’t know what this does and I have changed no options.
This should enable you to get your character walking around your nav mesh to random locations.
March 24, 2022 at 3:37 am #10556I know i’m a little late to the party, but change the or operand to and (|| to &&) in your while statement. Otherwise, the code works well.
`} while ((Vector3.Distance(ai.Kinematic.Position, loc) < 1f) && !ai.Navigator.GetPathTo(loc, 10, out myPath));
March 24, 2022 at 3:38 am #10557yeah, that code tag didn’t work, just add in, it’s pretty obvious where I did
for clarity, here’s the line without the `:} while ((Vector3.Distance(ai.Kinematic.Position, loc) < 1f) || !ai.Navigator.GetPathTo(loc, 10, out myPath));
March 24, 2022 at 5:39 am #10561And because I apparently can’t edit my posts, i guess i’ll triple post now heh.
http://pastebin.com/WhFS9LNJHere’s an updated script. I reduced the range on the x coords to be 0-8, still keeping the minimum of 1. This keeps your characters walking *more* forward - if you have root motion on an animation, this will keep it from being jittery, as it won’t be trying to path to a location behind it. This will also reduce the amount of time your AI will spend spinning in circles.
my BT is set up as zcoldrick had done.
-
AuthorPosts
You must be logged in to reply to this topic.