crashkonijn / GOAP

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

Bug: AgentMoveBehaviour Events not firing #283

Closed mattemoore closed 1 week ago

mattemoore commented 2 weeks ago

Describe the bug The various target related events in my agent move behaviour script are not firing. Awake and OnEnable events fire. None of the target events fire thus shouldMove is always false.

Steps to Reproduce Steps to reproduce the behavior: Followed tutorial. Modified to my needs as needed.

Expected behavior Target events fire which trigger agent movement.

Screenshots image image image

using Assets.Scripts;
using CrashKonijn.Agent.Core;
using CrashKonijn.Agent.Runtime;
using UnityEngine;

namespace Assets.Scripts.GOAP
{
    public class AgentMoveBehaviour : MonoBehaviour
    {
        PlayerController _playerController;
        private AgentBehaviour agent;
        private ITarget currentTarget;
        private bool shouldMove;

        private void Awake()
        {
            Debug.Log($"Awake");
            _playerController = GetComponent<PlayerController>();
            this.agent = this.GetComponent<AgentBehaviour>();
        }

        private void OnEnable()
        {
            Debug.Log($"OnEnable");
            this.agent.Events.OnTargetInRange += this.OnTargetInRange;
            this.agent.Events.OnTargetChanged += this.OnTargetChanged;
            this.agent.Events.OnTargetNotInRange += this.TargetNotInRange;
            this.agent.Events.OnTargetLost += this.TargetLost;
        }

        private void OnDisable()
        {
            Debug.Log($"OnDisable");
            this.agent.Events.OnTargetInRange -= this.OnTargetInRange;
            this.agent.Events.OnTargetChanged -= this.OnTargetChanged;
            this.agent.Events.OnTargetNotInRange -= this.TargetNotInRange;
            this.agent.Events.OnTargetLost -= this.TargetLost;
        }

        private void TargetLost()
        {
            Debug.Log($"TargetLost");
            this.currentTarget = null;
            this.shouldMove = false;
        }

        private void OnTargetInRange(ITarget target)
        {
            Debug.Log($"OnTargetInRange");
            this.shouldMove = false;
        }

        private void OnTargetChanged(ITarget target, bool inRange)
        {
            Debug.Log($"OnTargetChanged");
            this.currentTarget = target;
            this.shouldMove = !inRange;
        }

        private void TargetNotInRange(ITarget target)
        {
            Debug.Log($"TargetNotInRange");
            this.shouldMove = true;
        }

        public void Update()
        {

            if (!this.shouldMove)
            {

                return;
            }

            if (this.currentTarget == null)
            {

                return;
            }

            Vector3 directionToTarget = _playerController.Puck.transform.position - this.transform.position;
            _playerController.Skate(directionToTarget);
        }

        private void OnDrawGizmos()
        {
            if (this.currentTarget == null)
                return;

            Gizmos.DrawLine(this.transform.position, this.currentTarget.Position);
        }
    }
}

Unity Version 2022.3.35f1

Unity.Collections Version Not sure how to retrieve this.

Package Version In screenshots section.

Additional context Additional context available upon request.

mattemoore commented 1 week ago

Added in screenshots from inspector.

image image image

mattemoore commented 1 week ago

Tried the scriptables approach and same issue: image image image image image

crashkonijn commented 1 week ago

Hey!

The logs on the GoapActionProvider show that it was unable to find a valid action to perform.

Your goal already has its conditions met and is already completed and as such won't search for anything to do.

Since it doesn't find a valid action, you're not getting any move events 😁

mattemoore commented 1 week ago

Fascinating. I was looking at the goal condition in the opposite way. I was looking at the goal condition as when the goal would be active not completed and thus not "enabled".

crashkonijn commented 1 week ago

Glad we figured it out!

I should probably spend some more time explaining this in the docs.