Alternate range detection

I used a slightly different approach to determining the range at which to stop before the target, by creating an offset variable, instead of a bool. It worked perfectly, and I didn’t run into the Null reference error.

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour
    {
        Transform target;
        CharacterMovement move;
        [SerializeField] float weaponRange = 2f;

        private void Start()
        {
             move = GetComponent<CharacterMovement>();
        }
        private void Update()
        {
            if(target != null)
            {
                float offset = Vector3.Distance(target.position, transform.position);
                move.MoveTo(target.position);
                if (offset < weaponRange)
                { move.Stop(); }
            }
            
        }
        public void Attack(CombatTarget combatTarget)
        {
            target = combatTarget.transform;
            print("Take that, you scurvy dog!");
        }
        public void CancelTarget()
        {
            target = null;
        }
    }
}
3 Likes

Good job

Privacy & Terms