News Forums RAIN General Discussion and Troubleshooting How to check point is on the nav mesh?

This topic contains 22 replies, has 4 voices, and was last updated by  abchiptop 1 year, 9 months ago.

Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #8565

    prime
    Keymaster

    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.)

    #8567

    zcoldrick
    Participant

    Could you suggest what code I could use to test if there is a valid path to a particular point?

    #8640

    prime
    Keymaster
    #8668

    zcoldrick
    Participant

    Could you give some context to that API.

    I don’t know how to use the out variable.

    #8679

    zcoldrick
    Participant

    The 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
    — move

    The 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.

    #10556

    abchiptop
    Participant

    I 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));

    #10557

    abchiptop
    Participant

    yeah, 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));

    #10561

    abchiptop
    Participant

    And because I apparently can’t edit my posts, i guess i’ll triple post now heh.
    http://pastebin.com/WhFS9LNJ

    Here’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.

Viewing 8 posts - 16 through 23 (of 23 total)

You must be logged in to reply to this topic.