My Solution to getting the `CombatTarget`

This removes the requirement to have a constant Physics.RaycastAll() which can be expensive. The FindObjectOfType<PlayerController>() is also not recommended unless there is only one PlayerController in the scene.

    public class CombatTarget : MonoBehaviour
    {
        private void OnMouseDown()
        {
            FindObjectOfType<PlayerController>().target = this;
        }
    }

// In PlayerController
        public CombatTarget target;

        private void InteractWithCombat()
        {
            if (target)
            {
                _agent.SetDestination(target.transform.position);
            }

            if (target && (target.transform.position.z - transform.position.z) <= _agent.stoppingDistance)
            {
                GetComponent<Fighter>().Attack();
            }
        }

1 Like

While this will work for now, it won’t work in the last section when we start introducing affordances (where the cursor changes based on the actions that can be done if you click right now). We’ll be introducing an interface called IHandleRaycast for this purpose.

2 Likes

Privacy & Terms