News › Forums › RAIN › General Discussion and Troubleshooting › Problem with following the player
This topic contains 12 replies, has 2 voices, and was last updated by Draco Nared 2 weeks, 2 days ago.
-
AuthorPosts
-
March 11, 2022 at 6:27 am #36571
Hi!
First of all, I want to say that RAIN is magnificent tool and spared me a lot of trouble making AI on Unity’s builtin Navmesh. However, I stumbled upon a major issue that I have no idea how to cope with.
My AIPawn uses a script in which it moves to a last location of player when it loses him/her from its sight. Usually it works, but there are situations when the chosen path to the target is a floor below. In the first pic you can see that DrawPaths chose a path a floor below, while in the second it is trying to move through a wall.
Sometimes it chooses a way around, going down to the floor below and then to the target (like in the first pic).
There are no issues in the Behaviour Tree, in 2D environment (only one floor) it works great. However, in situations like this, it goes nuts. Also, there is only one big navmesh on scene.Here’s the fragment of code that sets the location for the AIPawn.
public class MoveToLastKnownPosition : RAINAction { public override void Start(RAIN.Core.AI ai) { base.Start(ai); } public override ActionResult Execute(RAIN.Core.AI ai) { GameObject go = GameObject.FindGameObjectWithTag("Player"); ai.WorkingMemory.SetItem<Vector3>("moveToLocation", go.transform.position); return ActionResult.SUCCESS; } public override void Stop(RAIN.Core.AI ai) { base.Stop(ai); } }
I don’t know what to do so I am asking for your help.
March 11, 2022 at 7:36 am #36572[Update]
Small update on my problem. I tested this behaviour on prepared scene and it behaves weird. When I escape to the bottom floor, on each frame DrawPath switches between two paths (images below). Maybe the problem lies in my code? Any suggestions?
March 11, 2022 at 8:53 am #36584Do you have a sample project you can send that demonstrates the issue? Also, what version of RAIN?
March 11, 2022 at 9:00 am #36586Not at the moment - there’s a lot of stuff that I need to clean up first. I’ll probably post it tomorrow. Version 2.1.10
March 11, 2022 at 2:50 pm #36596I can’t really tell from the scene, but it looks a little bit like you are detecting the player and then moving there. And possibly that as soon as you start moving, you lose sight of the player and the AI changes course. Possible?
March 11, 2022 at 3:44 pm #36598AIPawn patrols. When it sees the player, it starts to follow the player. The player hides, Custom Action with MoveToLastKnownPosition executes and then it goes to the position it gets from this action. Something like this:
-PlayerVisual != null
—isSearching = true
—Move (Target: PlayerVisual)
-PlayerVisual == null
—if(isSearching)
—-Sets last position of Player to MoveToLocation (Custom Action with MoveToLastKnownPosition script)
—-Move (Target: MoveToLocation)- This reply was modified 2 weeks, 3 days ago by Draco Nared.
March 11, 2022 at 4:08 pm #36600Hmm. I still probably need to see an example project to really help…
I wonder, though, if this would work better if you didn’t use a custom action. Instead set up your detect like this:
SEQUENCER (repeat forever)
— DETECT (detectedPosition)
— Expression (playerPosition = detectedPosition) (evaluate to Success)and then always move to playerPosition. The playerPosition variable will always hold the last detected position, and will only get updated when the detect node succeeds.
March 12, 2022 at 1:30 am #36602As I told you earlier, BT works fine: there are no problems with detection of Player, moving towards Player and moving to place where Player disappeared; there are no issues, AIPawn does exactly what it is supposed to do. The only problem I have is with generating path to the last known position of Player when Player is no longer detected by AI Visual Sensor.
I tested the behaviour again and my custom action is (I’m 90% sure) not where the problem lies. However, it turned out that the second issue (with switching graphs) was happening due to my stupidity - the planes were on IgnoreRaycast layer. xD When I changed it to Default, all started to work perfectly.
On the other hand, the problem with switching graphs showed me that Graphs were drawn on navmesh on both planes (simultaneously). When Player moves to the bottom plane, I think AIPawn should move on the graph leading to the bottom plane, not try to draw second graph on the top plane as well. I don’t know why it does it - it’s like it wants to move to Player location with ignoring the Y parameter of Player’s Vector3 position. I can send you the sample project with this strange behaviour if you want.What’s more, when I was looking for the possible explanations (and probable causes of this strange graph generation) in the problem described in my first post, I changed how Navmesh was generated and this way I had third layer of navmesh in the section of the scene. The navmesh generated a layer on the ceiling between two floors:
Navmesh layer
Floor
Navmesh layer
Ceiling
Navmesh layer
Floor
… what made me thinking that the cause of this problem may be hidden somewhere in the scene. It also showed me why waypoints where behaving strangely there (they were dropping to the surface of ceiling’s collision, not the first floor’s). I hope that I will find it because, due to the fact that there are lots of assets, I can’t send you a sample scene of this problem at the moment - I would have to switch many things to Unity’s cubes and planes and capsules etc.- This reply was modified 2 weeks, 2 days ago by Draco Nared.
- This reply was modified 2 weeks, 2 days ago by Draco Nared.
March 12, 2022 at 2:43 am #36607I think I might have found the cause - it was navmesh generation settings.
It turned out that Player’s collision radius was smaller than Navmesh’s Walkable Radius and Player could enter areas where there was no Navmesh on. When I entered such area, AIPawn detected me but decided to go to the bottom floor which had Navmesh in the same XZ spot, ignoring the Y parameter (like Vec3(0,2,0) and Vec3(0,0,0) was the same for it). Here’s the visualisation:
As you can see, Player is standing on unwalkable cube, so AIPawn, which detected Player, walks to the spot right below Player.So, the solution would be to regenerate the Navmesh with Player’s collision radius or to make Player’s collision bigger (or both). Anyway, that might solve my problem, will tell later after testing.
- This reply was modified 2 weeks, 2 days ago by Draco Nared.
March 12, 2022 at 6:21 am #36614A couple of notes:
1) I wasn’t trying to suggest that the problem you are seeing would be solved by a change in the behavior tree. I was just pointing out that you are unnecessarily dropping into custom code.
2) Sounds like this problem wouldn’t occur if the player stepped onto an unwalkable area on the bottom floor. Only if the player steps onto an unwalkable area on the top floor that has a walkable area directly below it. Is that true?
March 12, 2022 at 7:11 am #366161) Sorry for misunderstanding you, then.
2) I tried to do that aaaand… guess what? AIPawn walked upstairs to the top floor. Seems like it doesn’t mind Y parameter as long as XZ are ok.March 12, 2022 at 7:46 am #36618We’ll spend some time thinking about the best way to solve this issue. I understand that you have a workaround for now (essentially keeping the player on the navmesh too) but there should be a way to keep the pathfinder from choosing the wrong path, as in your screenshot.
The issue has to do with determining a max vertical offset for locating an object. Since RAIN allows objects to be significantly above the navmesh, it assumes the player is (from a nav perspective) actually located on the ground floor in your example, because that’s the nearest navmesh location directly below the player.
March 12, 2022 at 11:11 am #36626Yup, I have a workaround - just keeping the player inside navmesh.
-
AuthorPosts
You must be logged in to reply to this topic.