Cannot move player after Mover.stop

After following each step of the course, I noticed that in this chapter, my player won’t move after the player is approaching the enemy. So, when I debug this using Debug.Log, I noticed that Mover.Stop() was always called. Whenever I click another terrain, my player didn’t move at all. I also check every line at the github commit and made sure that my code was exactly the same. This is the log when Mover.Stop() f function was called. By the way great course as always!

There might be just a little ! or other bit of logic hiding in the code that can be easy to miss when comparing to the git version.
Paste in your Mover.cs, Fighter.cs and PlayerController.cs scripts and we’ll take a look at them to see what might be the issue. You can format the code by putting ``` (three backwards quotes next to the 1 on your keyboard) on it’s own line, and then pasting in the text of the code on the line after that. End the code with three more ``` on their own line.

Yeah I finally found the issue. Back then I realized that the target was always set to the enemy, so I need to nullify the target before moving into the next step.

using RPG.Movement;
using UnityEngine;

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

        private void Update()
        {
            if (target == null) return;
            print(target);
            bool isInRange = Vector3.Distance(transform.position, target.position) < weaponRange;
            if (target != null && !isInRange)
            {
                GetComponent<Mover>().MoveTo(target.position);
                print("Move Called");
            }
            else
            {
                GetComponent<Mover>().Stop();
                target = null;
                print("Stop Called");
            }
        }

        public void Attack(CombatTarget combatTarget)
        {
            target = combatTarget.transform;
            // print("Attack called.");
        }
    }
}
5 Likes

Good job finding that!

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

Privacy & Terms