News › Forums › RAIN › General Discussion and Troubleshooting › Detect closest target
This topic contains 5 replies, has 2 voices, and was last updated by colosso 6 months, 1 week ago.
-
AuthorPosts
-
January 27, 2023 at 7:38 am #39982
Hi guys. I’m pretty new to RAIN but its working like a charm so far. I’ve made a hero with an AI which should attack towers that are in front of him. Unfortunately, the AI is not targeting the towers after their distance to the hero. He attacks the targets in the order the entities were added. The sensors and entities are both visual. I added a NearestXfilter to the sensor with the value 1 but it is not changing anything… Am I doing something wrong or is there another way to do this?
January 27, 2023 at 1:23 pm #39984You aren’t doing anything wrong, the sensors have a bug in them that keep them from getting the correct node when they are set to Best.
It is fixed in the next version, but until then, use this:
using RAIN.Entities.Aspects; using RAIN.Perception.Sensors; using RAIN.Serialization; [RAINSerializableClass] public class FixedVisualSensor : VisualSensor { public override void Sense(string aAspectName, MatchType aMatchType) { if (aMatchType == MatchType.BEST) { base.Sense(aAspectName, MatchType.ALL); float tBestScore = float.MaxValue; RAINAspect tBestAspect = null; for (int i = 0; i < _matches.Count; i++) { float tScore = Score(_matches[i]); if (tScore < tBestScore) { tBestScore = tScore; tBestAspect = _matches[i]; } } _matches.Clear(); if (tBestAspect != null) _matches.Add(tBestAspect); } else base.Sense(aAspectName, aMatchType); } }
That will add a new sensor to the drop down that fixes the issue. That will make sure it initially picks the closest one instead of the first added. If you want the AI to be able to switch targets based on the closest one you will also need to uncheck consistent on the detect node.
January 27, 2023 at 8:09 pm #39991Wow thank you so much. Works perfectly
January 30, 2023 at 2:33 am #39999Sorry to correct myself… Unfortunately almost perfectly In the builds we unfortunately get now following error:
FieldSerializer: Couldn't deserialize type: System.Type (FixedVisualSensor) NullReferenceException: Object reference not set to an instance of an object.
Any idea what could cause that?
January 30, 2023 at 3:11 pm #40000So just to be clear, you are saying it was working in the editor but it throws that error in any standalone builds right?
Sounds like it isn’t getting included in the build, is it possible it is in a special folder by chance? Like Editor, or Plugins, or similar?
I ran a quick test with it and didn’t have any issues with the standalone PC build I did. You may want to do a quick test in a nearly empty scene, with one AI using that sensor, and see if it still causes problems.
January 30, 2023 at 9:11 pm #40003Thank you very much, you are right again. I had it in the Rain editor folder seems like that caused it. Putting it in the main asset folder fixed it Thank you very much you saved my day again!
-
AuthorPosts
You must be logged in to reply to this topic.