News › Forums › RAIN › General Discussion and Troubleshooting › Game Freeze when AI on an NPC
Tagged: Custom Action, freezes, tutorial
This topic contains 7 replies, has 2 voices, and was last updated by Sigil 2 months, 2 weeks ago.
-
AuthorPosts
-
December 1, 2022 at 8:54 pm #39588
I have been having this issues and couldn’t figure out what was causing it till i made a simple scene and just kept disabling things. I got to the point where when the AI is enabled on the NPC and I touch the npc with my fps character, the game with just crash/freeze…(On play in editor, and on built mode) I have to end the process and restart unity. When I disable the AI its fine and game does not crash.
I followed a tutorial for setting up my AI so I have no idea what is wrong. Any ideas??
The behavior tree i used is in this tutorial. I followed this to the T and it works great, but not if the NPC/enemy gets up in my face. And its a Zombie…. So…December 3, 2022 at 4:29 pm #39609I am guessing (I can’t look at the video at the moment), but I believe there is a custom action used in that tutorial which either uses a while loop or a do while loop. It may be one for choosing a random target or location, I can’t recall exactly. Anyhow, I believe that custom action is the culprit, and if you post the code here I can help you fix it.
What happens is that the loop can’t find a random target and ends up running forever, which freezes up Unity.
December 3, 2022 at 4:51 pm #39612Have any idea where I can find the Code?? LOL I am looking at the Tree but forgot where the actual code goes.
December 3, 2022 at 4:55 pm #39614Found it!!! This is the action.
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 WanderLocation() { actionName = "WanderLocation"; } public override void Start(RAIN.Core.AI ai) { base.Start(ai); _time += Time.time; } 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.MaxHeightOffset , 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 > 500f) { ai.WorkingMemory.SetItem("isSearching", false); _time = 0; } return ActionResult.SUCCESS; } public override void Stop(RAIN.Core.AI ai) { base.Stop(ai); } }
December 6, 2022 at 9:30 am #39618Have you had a chance to check out this code?? I tried getting rid of the action and not even having the extra wonder location, I don’t need it, but when i get rid of the extra parts in the tree, for some reason when my enemy goes to attack me, it gets stuck at the attack anim. and just keeps attacking thin air. Not sure why that would happen. But I really only need my enemy to follow a waypoint, then to detect the player at certain distance and run at me and then attack when close. If i get out of range again it would just go back to the waypoint. Any ideas??
December 7, 2022 at 12:46 pm #39623Sorry it took so long to get back, so with the code you posted this is the problem area:
... 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.MaxHeightOffset , NavigationManager.GraphType.Navmesh , ((BasicNavigator)ai.Navigator).GraphTags); } while ((Vector3.Distance(ai.Kinematic.Position, loc) < 2f) || (found.Count == 0)); ...
So that little bit of code is looking for a random location within an 8 meter radius, but further than 2 meters away, on any RAIN navigation mesh that the AI can find. The problem occurs if there isn’t a navigation mesh or if it has a lot of holes, then the loop can take a long time to run or run forever (locking up Unity). You could do something like this instead:
... Vector3 loc = Vector3.zero; List<RAINNavigationGraph> found = new List<RAINNavigationGraph>(); for (int i = 0; i < 10 && (Vector3.Distance(ai.Kinematic.Position, loc) < 2f || found.Count == 0); i++) { 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.MaxHeightOffset, NavigationManager.GraphType.Navmesh, ((BasicNavigator)ai.Navigator).GraphTags); } ...
So that will try 10 times to find a random location and then just take whatever the last random location was if it can’t find anything. You’d want to generate a RAIN navigation mesh in the scene to get a better result for it though.
As for your actual requirements that you posted after, I recommend taking a look at the starter kit we have, which will give you some examples of waypoint following and detecting. And then take a look at this thread where I went through a lot of details on creating a patrol/attack/follow AI.
December 10, 2022 at 5:22 pm #39671Another ? that does not pertain to this, instead of making a new thread i’ll post it here. I want to use Root Motion on my zombies, otherwise they look funny. How would I change my behavior tree so that root motion works right?? Right now I have it working, but had to set the walk speed to .1 otherwise they would walk into the walls for some reason. But they still don’t look right. Its hard to explain what they are doing now unless i make a video…If you know what i’m talking about and know of a fix, let me know, otherwise i can get a video.
They walk ahead but kinda rotate a tiny bit while they are walking….. small amount but enough to be annoying.December 10, 2022 at 6:25 pm #39672So since you started with that tutorial, I imagine you may have your animations being played from your behavior tree, is that correct?
-
AuthorPosts
You must be logged in to reply to this topic.