Taking the RPG class - character keeps coming back to target

I have gone through the dependency breaking classes three times - must have missed something - can you make a suggestion here?

Fighter script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Move;
using RPG.Core;


namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        [SerializeField]
        float weaponRange = 2f;
        Transform target;
        private void Update()
        {
            if (target == null) return;

            if (!GetInRange())
            {
                GetComponent<Mover>().Moveto(target.position);
            }
            else
            {
                GetComponent<Mover>().Cancel();
            }
        }
         private bool GetInRange()
        {
            return Vector3.Distance(transform.position, target.position) < weaponRange;
        }
        public void Attack(CombatTarget combatTarget)
        { 
           target = combatTarget.transform;
          }
        public void Cancel()
        {
            target = null;
        }
    }

}
Mover script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using RPG.Core;

namespace RPG.Move
{
    public class Mover : MonoBehaviour, IAction
    {
        [SerializeField]
        private Transform target;
        NavMeshAgent navMeshAgent;

        private void Start()
        {
            navMeshAgent = GetComponent<NavMeshAgent>();
        }
        void Update()
        {

            UpdateAnimator();
        }
   
       public void Cancel()
        {
            navMeshAgent.isStopped = true;
        }

        public void StartMoveAction(Vector3 destination)
        {
            GetComponent<ActionScheduler>().StartAction(this);
              Moveto(destination);

        }
        public void Moveto(Vector3 destination)
        {
            navMeshAgent.destination = destination;
            navMeshAgent.isStopped = false;
        }

        private void UpdateAnimator()
        {
            Vector3 velocity = navMeshAgent.velocity;
            Vector3 localVelocity = transform.InverseTransformDirection(velocity);
            float speed = localVelocity.z;
            GetComponent<Animator>().SetFloat("ForwardSpeed", speed);
        }
    }

}
Action Scheduler script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Core

{
    public class ActionScheduler : MonoBehaviour
    {
        IAction currentAction; 
        public void StartAction(IAction action)
        {
            if (currentAction == action) return;
            if (currentAction != null)
            {
                currentAction.Cancel();
            }
                currentAction = action;
            
        }
    }
}

Also the player has the mover, fighter and actionscheduler scripts attached.

Thanks!

Quick tip: When posting code, type the backwards apostrophe three times (that’s the key next to the 1 on your keyboard) and press enter, then paste your code and close it with another three backwards apostrophes one one line (or select the code and press the </> button.

Your Fighter’s Attack() method isn’t calling ActionScheduler.StartAction(); This means that even when mover properly calls it, Fighter’s never registered as the current action and never gets the Cancel method.

Add this to the beginning of Fighter’s Attack() method:

GetComponent<ActionScheduler>().StartAction(this);

Sorry - I’m new here and where I usually go, I guess it lazily does what I was supposed to do. Thanks though! I had both scripts to the class project and my game open late at night and accidentally deleted it from the class script. Thank you so much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms