News Forums RAIN General Discussion and Troubleshooting Refusing Waypoint Network & Fail Parallel

This topic contains 8 replies, has 2 voices, and was last updated by  Velathora 2 months, 3 weeks ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #34733

    Velathora
    Participant

    Hello all,

    I’ve been following along @CodersExpo ‘s tutorials and had no issues until the last and final tutorial.

    I seem to be having issues with the custom action and the wander.

    Basically, I am able to get the character to run at the hero, wave (instead of punch) when within the “near” detector range, and whilst within his normal patrol route, idle randomly within the timer intervals set in BT.

    Afterwards, we had been advised to get the custom action created to allow the character to wander after the Hero takes cover and removes himself from line of sight. At that point, I have the parallel node showing a failure and the waypoint path node showing a failure as well. It seems odd because it states that the execution of the previous “WanderLocation” custom action is succeeding as needed.

    For reference,

    BT:

    ->PAR-RPT:Never, Fail:Any, Succeed:Any, TieBreaker:Fail
    ->->Detect-Sensor:"eyes", Aspect:"aHero", FormVariable:varHero
    ->->Detect-Sensor:"close", Aspect:"aHero", FormVariable:varNear
    ->->Selector
    ->->->Constraint(TreeHereWorksPerfectly)
    ->->->Constraint(TreeHereWorksPerfectly)
    ->->->Constraint-Constraint:varHero == null && isSearching (shows fail)
    ->->->->Custom Action - Name:WanderLocation, Assembly:(global), Class:WanderLocation (Succeeds)
    ->->->->PAR - RPT:Never, Fail:Any, Succeed:Any, TieBreaker:Succeed (Fails)
    ->->->->->WaypointPath - WaypointNetwork:"Wander", PathTarget:varNext, MoveTargetVariable:varMoveTo (Fails)
    ->->->->->->Move-MoveTarget:varMoveTo, MoveSpeed:3 (NeverCalled)
    ->->->->->Animate - AnimationState:Walk (fails)

    Custom Action Script “WanderLocation”:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    using RAIN.Navigation;
    using RAIN.Navigation.Graph;
    [RAINAction]
    public class WanderLocation : RAINAction
    {
    		private static float _time = 0f;
    		public override void Start (RAIN.Core.AI ai)
    		{
    				base.Start (ai);
    				_time += Time.time;
    		}
    		public WanderLocation ()
    		{
    				actionName = "WanderLocation";
    		}
    		public override ActionResult Execute (RAIN.Core.AI ai)
    		{
    				Vector3 loc = Vector3.zero;
    				List<RAINNavigationGraph> found = new List<RAINNavigationGraph> ();
    				do {
    						loc = new Vector3 (ai.Kinematic.Position.x + Random.Range (-8f, 8f),
    			                  ai.Kinematic.Position.y,
    			                  ai.Kinematic.Position.z + Random.Range (-8f, 8f));
    						found = NavigationManager.Instance.GraphsForPoints (ai.Kinematic.Position, 
    			                                                    loc, 
    			                                                    ai.Motor.StepUpHeight, 
    			                                                    NavigationManager.GraphType.Navmesh,
    			                                                    ((BasicNavigator)ai.Navigator).GraphTags);
    				} while((Vector3.Distance(ai.Kinematic.Position, loc)<2f)||(found.Count == 0));
    				ai.WorkingMemory.SetItem<Vector3> ("varMoveTo", loc);
    				if (_time > 5000f) {
    						ai.WorkingMemory.SetItem ("isSearching", false);
    				}
    				return ActionResult.SUCCESS;
    		}
    		public override void Stop (RAIN.Core.AI ai)
    		{
    				base.Stop (ai);
    		}
    }

    So essentially, the character will run on the spot after losing detection and is set to “isSearching”, and after 5 seconds or so, he will then return to the normal patrol route. This is unexpected behaviour, simply because he should instead be wandering around searching on the waypoint network for signs of the hero.

    Would really appreciate the assistance to wrap my head around RAIN AI.

    Cheers.

    • This topic was modified 2 months, 3 weeks ago by  Velathora. Reason: Cleaned
    • This topic was modified 2 months, 3 weeks ago by  Velathora. Reason: Clarify end
    #34738

    prime
    Keymaster

    I’m not certain, but it looks to me like you have your variables mixed up in your Waypoint node. The wander script is setting your destination into the varMoveTo variable. In your waypoint node that should be your PathTarget - the place you are trying to create a path to. The MoveTargetVariable is the name of the variable you are passing to the move node inside the waypoint node. So:
    - use varMoveTo as your PathTarget
    - use varNext as your MoveTargetVariable
    - make sure your move node Move Target is set to varNext

    #34739

    Velathora
    Participant

    Seems to work.

    Cheers.

    • This reply was modified 2 months, 3 weeks ago by  Velathora. Reason: AddIssueNextPost
    #34742

    Velathora
    Participant

    I stand corrected, it works as expected now, but it seems to crash after detection on the second or third time.

    Any ideas?

    #34744

    prime
    Keymaster

    When you say “crash”, what do you mean? Unity hangs/crashes? You get errors in the console?

    #34745

    prime
    Keymaster

    This is a better Wander point chooser. User Repeat Until Success to ensure it chooses a valid wander position.

    using UnityEngine;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using RAIN.Action;
    using RAIN.Core;
    using RAIN.Representation;
    using RAIN.Navigation;
    [RAINAction("Choose Wander Position")]
    public class ChooseWanderPosition : RAINAction
    {
        /// <summary>
        /// Public Expressions are editable in the Behavior Editor
        /// WanderDistance is the max range to use when picking a wander target
        /// </summary>
        public Expression WanderDistance = new Expression();
        /// <summary>
        /// Public Expressions are editable in the Behavior Editor
        /// StayOnGraph is a boolean (true/false) that indicates whether the wander target must be on the nav graph
        /// </summary>
        public Expression StayOnGraph = new Expression();
        /// <summary>
        /// Public Expressions are editable in the Behavior Editor
        /// WanderTargetVariable is the name of the variable that the result will be assigned to
        /// *Don't use quotes when typing in the variable name
        /// </summary>
        public Expression WanderTargetVariable = new Expression();
        /// <summary>
        /// The default wander distance to use when the WanderDistance is invalid
        /// </summary>
        private float _defaultWanderDistance = 10f;
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            if (!WanderTargetVariable.IsVariable)
                throw new Exception("The Choose Wander Position node requires a valid Wander Target Variable");
            float tWanderDistance = 0f;
            if (WanderDistance.IsValid)
                tWanderDistance = WanderDistance.Evaluate<float>(ai.DeltaTime, ai.WorkingMemory);
            if (tWanderDistance <= 0f)
                tWanderDistance = _defaultWanderDistance;
            Vector3 tDirection = new Vector3(UnityEngine.Random.Range(-1f, 1f), 0f, UnityEngine.Random.Range(-1f, 1f));
            tDirection *= tWanderDistance;
            Vector3 tDestination = ai.Kinematic.Position + tDirection;
            if (StayOnGraph.IsValid && (StayOnGraph.Evaluate<bool>(ai.DeltaTime, ai.WorkingMemory)))
            {
                if (NavigationManager.Instance.GraphForPoint(tDestination, ai.Motor.DefaultStepUpHeight).Count == 0)
                    return ActionResult.FAILURE;
            }
            ai.WorkingMemory.SetItem<Vector3>(WanderTargetVariable.VariableName, tDestination);
            return ActionResult.SUCCESS;
        }
    }
    #34746

    Velathora
    Participant

    Sorry, Unity hangs/crashes.

    Sits for about one or two minutes and then crashes with no code to identify.

    Something must be continually running, Unity crashes and identifies 1.1GB usage.

    And the BT is the only thing I’ve touched within the project that could cause this, my logic must be wrong.

    • This reply was modified 2 months, 3 weeks ago by  Velathora.
    #34748

    prime
    Keymaster

    The older Wander script you are using can cause that problem. Try the new one I posted.

    #34749

    Velathora
    Participant

    Works perfectly.

    Thank you again!

    Cheers.

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.