crashkonijn / GOAP

A multi-threaded GOAP system for Unity
Apache License 2.0
1.16k stars 127 forks source link

Question: TargetSensor doesn't update #286

Closed mattemoore closed 1 week ago

mattemoore commented 1 week ago

Expected behavior Target Sensor is created with proper position but doesn't update as the GameObject it bases its position on moves.

Screenshots image

Code

using CrashKonijn.Agent.Core;
using CrashKonijn.Goap.Runtime;
using UnityEngine;

namespace Assets.Scripts.GOAP
{
    public class LoosePuckSensor : LocalTargetSensorBase
    {
        GameController _gameController;
        Vector3 _puckPosition;
        public override void Created()
        {
            _gameController = GameObject.Find("GameController").GetComponent<GameController>();
        }

        // Is called every frame that an _agent of an `AgentType` that uses this sensor needs it.
        // This can be used to 'cache' data that is used in the `Sense` method.
        // Eg look up all the trees in the scene, and then find the closest one in the Sense method.
        public override void Update()
        {
            _puckPosition = _gameController.Puck.transform.position;
            Debug.Log(_puckPosition);
        }

        public override ITarget Sense(IActionReceiver agent, IComponentReference references, ITarget existingTarget)
        {
            if (_gameController == null) return null;
            if (_gameController.Puck == null) return null;
            if (existingTarget is PositionTarget positionTarget)
            {
                return positionTarget.SetPosition(_puckPosition);
            }
            return new PositionTarget(_puckPosition);
        }
    }
}

Package Version v3 beta

Additional context Add any other context about the problem here.

mattemoore commented 1 week ago

I was using PositionTarget instead of TransformTarget. https://goap.crashkonijn.com/goap-v3/classes/sensors#targetsensor