News Forums RAIN General Discussion and Troubleshooting Detect closest target

Tagged: ,

This topic contains 5 replies, has 2 voices, and was last updated by  colosso 6 months, 1 week ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #39982

    colosso
    Participant

    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?

    #39984

    Sigil
    Keymaster

    You 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.

    #39991

    colosso
    Participant

    Wow thank you so much. Works perfectly

    #39999

    colosso
    Participant

    Sorry 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?

    #40000

    Sigil
    Keymaster

    So 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.

    #40003

    colosso
    Participant

    Thank 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!

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

You must be logged in to reply to this topic.