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();
}
}