News Forums RAIN General Discussion and Troubleshooting Detect without sensors?

Tagged: 

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

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40137

    christougher
    Participant

    I have a game that has one Player entity at a time. I’m trying to decrease CPU usage as it is pushing lower end devices to not function adequately… I want my enemies to always be aware of the Player which is a stationary turret even from long distances. So… basically detect player entity immediately and never have to rely on sensors for player detection… How best to do this? Thx
    Chris

    • This topic was modified 5 months, 1 week ago by  christougher.
    • This topic was modified 5 months, 1 week ago by  christougher.
    #40157

    Sigil
    Keymaster

    I would likely just keep the player in the AI memory and then create a custom action to do a distance check against the player.

    using UnityEngine;
    using RAIN.Action;
    using RAIN.Representation;
    [RAINAction]
    public class DirectSense : RAINAction
    {
        public Expression Player = new Expression();
        public Expression Distance = new Expression();
        private float _distanceSquared = 0f;
        public override void Start(RAIN.Core.AI ai)
        {
            if (Distance.IsValid)
            {
                _distanceSquared = Distance.Evaluate<float>(ai.DeltaTime, ai.WorkingMemory);
                _distanceSquared *= _distanceSquared;
            }
        }
        public override ActionResult Execute(RAIN.Core.AI ai)
        {
            if (Player.IsValid)
            {
                GameObject tPlayer = Player.Evaluate<GameObject>(ai.DeltaTime, ai.WorkingMemory);
                if (tPlayer != null)
                {
                    float tDistanceSquared = (tPlayer.transform.position - ai.Body.transform.position).sqrMagnitude;
                    if (tDistanceSquared <= _distanceSquared)
                        return ActionResult.SUCCESS;
                }
            }
            return ActionResult.FAILURE;
        }
    }

    So this assumed you added the player into the AI’s memory as a GameObject (memory is the second tab on the AI) and passed it on to this custom action in your behavior tree.

    #40159

    christougher
    Participant

    But I will need to add the Player into the AI’s memory programmatically… I can’t manually assign it as I’m using different prefabs for both enemies and player objects and I have different levels requiring that they be able . But once the scene is loaded it will be only one player staying in the same location. So is it possible for an entity to be fed into AI’s memory without the use of expensive sensors?
    I’ve poored a lot of time into the different AI’s tweaking, perfecting, pulling out hair etc. But I’ve only recently discovered the Profiler Window in Unity and its showing me just how much juice RAIN is using and its scary….I have to cut way back on RAIN, it is sometimes taking 80% of my CPU usage…

    #40160

    Sigil
    Keymaster

    You can assign anything you like to an AI at runtime, you’ll need to grab their memory and set the value in it. Since you are bypassing the sensor system, there is no need to use an entity, you can just feed the player directly.

    // I'm assuming I'm on a component on the player and already got a list of AI GameObjects
    for (int i = 0; i < tAIObjects.Count; i++)
        tAIObjects[i].GetComponentInChildren<AIRig>().AI.WorkingMemory.SetItem<GameObject>("player", gameObject);

    By the way, I can work with you on any performance issues you may have.

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

You must be logged in to reply to this topic.