News › Forums › Troubleshooting › AI not changing path via script
Tagged: AI, Behavior Tree, C#, unity, waypoint
This topic contains 6 replies, has 2 voices, and was last updated by TRALLALAL 5 months, 1 week ago.
-
AuthorPosts
-
July 18, 2022 at 8:56 pm #31933
Hello.
I want the AI to get to the closest cover when a member of the player team or the player itself is on its field of view, I made this script
using UnityEngine; using System.Collections; using System.Collections.Generic; using RAIN.Core; using RAIN.Action; [RAINAction] public class AIGunHandling : RAINAction { bool canChangeCover = true; Vector3 newCover; private AIRig currentAI; GameObject currentAIObject; //AI ai; public AIGunHandling() { actionName = "AIGunHandling"; } public override void Start(AI ai) { base.Start(ai); //currentAI = currentAI.GetComponentInChildren<AIRig>(); } public override ActionResult Execute(AI ai) { if (canChangeCover) { canChangeCover = false; //Vector3 cover = new Vector3(0, 0, 0); ai.WorkingMemory.SetItem<Vector3>("temporaryCover", findClosestCover(ai)); } return ActionResult.SUCCESS; } public override void Stop(AI ai) { base.Stop(ai); } Vector3 findClosestCover(AI ai) { float currentDistance = Mathf.Infinity; Vector3 temporaryCover = Vector3.zero; foreach (GameObject cover in GameObject.FindGameObjectsWithTag("Cover")) { if (Vector3.Distance(ai.Kinematic.Position, cover.transform.position) < currentDistance) { currentDistance = Vector3.Distance(ai.Kinematic.Position, cover.transform.position); temporaryCover = cover.transform.position; } } return temporaryCover; } }
But when the AI finds me it changes animation (there is a normal walking animation when it’s patrolling and a sprinting animation when it’s in combat status) but it follows the normal waypoint and it gets stuck at the middle, it doesn’t get stuck when he’s patrolling and he hasn’t found me.
This is the tree editor
http://i.gyazo.com/f7e3f121038d9ae4d1a145c8999fc5be.png
http://i.gyazo.com/35d6f11542fda494c77d4c57212084cb.png
http://i.gyazo.com/e120210a5604c5edf4f6b02b7e8cb263.png
I also get this error when it stops middleway on the waypoint
NullReferenceException: Object reference not set to an instance of an object
RAIN.BehaviorTrees.BTWaypointNode.Start (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTConstraintNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTSelectorNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.Minds.BasicMind.Think ()
RAIN.Core.AI.Think ()
RAIN.Core.AIRig.AIUpdate ()
RAIN.Core.AIRig.Update ()Thanks a lot!
- This topic was modified 5 months, 1 week ago by Jester.
July 18, 2022 at 11:20 pm #31944The find temporary cover function has some problems as it only works when only one object has “Cover” as tag but at least the AI now finds it, could someone please tell me what’s wrong with that function?
Also, one more problem.
When he finds the cover unless I am in front of him he doesn’t walkJuly 19, 2022 at 2:41 pm #31984Fixed, I was stupid, the player was not in its field of view
Now I’ve got another problem, I’ll write her so I don’t have to start another topic.
The behavior tree is not looping, I noticed actions never return true and sometimes I get the same error I posted above,
NullReferenceException: Object reference not set to an instance of an object
RAIN.BehaviorTrees.BTWaypointNode.Start (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTConstraintNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTSelectorNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTParallelNode.Execute (RAIN.Core.AI ai)
RAIN.BehaviorTrees.BTNode.Run (RAIN.Core.AI ai)
RAIN.Minds.BasicMind.Think ()
RAIN.Core.AI.Think ()
RAIN.Core.AIRig.AIUpdate ()
RAIN.Core.AIRig.Update ()If needed, this is the XML of the behavior tree
<behaviortree version=”1.1″ repeatuntil=”” name=”Patrol”><parallel tiebreaker=”fail” succeed=”all” repeatuntil=”running” name=”root” fail=”any”><detect sensor=”"Eyes"” repeatuntil=”running” name=”Player team in field of view” entityobjectvariable=”varHero” aspectvariable=”” aspectobjectvariable=”” aspect=”"Hero"” /><detect sensor=”"CloseCover"” repeatuntil=”running” name=”Cover is close to entity” entityobjectvariable=”closeCover” aspectvariable=”” aspectobjectvariable=”” aspect=”"Cover"” /><parallel tiebreaker=”fail” succeed=”all” repeatuntil=”running” name=”parallel” fail=”any”><selector usepriorities=”False” repeatuntil=”” name=”selector”><constraint repeatuntil=”” priority=”” name=”Wander around waypoints” constraint=”varHero == null”><parallel tiebreaker=”fail” succeed=”all” repeatuntil=”” name=”parallel” fail=”any”><waypointpatrol waypointsetvariable=”WP” waypointactiontype=”patrol” traversetype=”loop” traverseorder=”forward” repeatuntil=”” pathtargetvariable=”” name=”WP” movetargetvariable=”WP”><move turnspeed=”” repeatuntil=”” name=”Move to Waypoint” movetarget=”WP” movespeed=”5″ facetarget=”” closeenoughdistance=”” closeenoughangle=”” /></waypointpatrol><animate repeatuntil=”” name=”Walking animation” animationstate=”Soldier_Run” /></parallel></constraint><constraint repeatuntil=”” priority=”” name=”Enter combat status” constraint=”varHero != null”><parallel tiebreaker=”fail” succeed=”all” repeatuntil=”” name=”parallel” fail=”any”><action repeatuntil=”” parametervalues=”” parameters=”” namespace=”(global)” name=”Find closest cover(Script)” classname=”AIGunHandling” /><move turnspeed=”” repeatuntil=”” name=”Move to cover” movetarget=”temporaryCover” movespeed=”10″ facetarget=”” closeenoughdistance=”2″ closeenoughangle=”” /><animate repeatuntil=”” name=”Sprint animation” animationstate=”Soldier_Sprint” /></parallel></constraint><constraint repeatuntil=”” priority=”” name=”Get in cover” constraint=”closeCover != null”><parallel tiebreaker=”succeed” succeed=”all” repeatuntil=”” name=”parallel” fail=”any”><random repeatuntil=”” name=”Random action”><animate weight=”” repeatuntil=”” name=”Stay in cover” animationstate=”Soldier_Crouch_IdleTrans” /><animate weight=”” repeatuntil=”” name=”Get up and shoot” animationstate=”Soldier_Aim_Idle” /></random></parallel></constraint></selector></parallel></parallel></behaviortree>
- This reply was modified 5 months, 1 week ago by TRALLALAL.
July 19, 2022 at 4:18 pm #31989Since I cannot edit posts I’ll leave this right here, that XML version might be wrong as I had a node set to repeat forever but it shouldn’t be like that.
This one is the one I’m using now, still doesn’t work.
July 19, 2022 at 5:37 pm #31991Found out that it gives me that error as soon as the player is not in it’s field of view so it’s null, that’s the cause of him not starting to follow the waypoints again when he doesn’t see me.
This is the behavior tree (screenshot this time as it’s only one thing causing the problem)
July 21, 2022 at 10:07 pm #32112Hi, thanks for posting your behavior tree here (as well as your updates as you were figuring out the problems).
I can’t tell by the end here whether or not you are still having trouble. Can you post again and let me know if you still need help?
July 22, 2022 at 6:37 am #32130In the end I just changed the AI and everything is fine, thanks for your reply.
-
AuthorPosts
You must be logged in to reply to this topic.