Question regarding Episode 30 of: RPG Core Combat Creator @ Udemy

This episode deals with the fact that our player keeps running towards out CombatTarget even if we click somewhere else on the screen. The solution proposed in this Episode seem very inefficient and a round about way of doing it. But I am scared that if I do it another way that there is meaning behind the method chosen that i don’t see yet due to not knowing what is in future episodes.

This is the github repository for that episode if you need to review the solution suggested:

My Fighter.cs solution:

namespace Scripts.Combat
{
    public class Fighter : MonoBehaviour
    {
        [SerializeField] float weaponRange = 2f;
        
        Mover _mover;
        Transform _targetTransform;
        bool _hasCombatTarget = false;

        void Start()
        {
            AssignVariables();
        }

        void AssignVariables()
        {
            _mover = GetComponent<Mover>();
        }

        void Update()
        {
            if (!_hasCombatTarget) return;
            if (Vector3.Distance(_targetTransform.position, transform.position) > weaponRange)
            {
                _mover.MoveTo(_targetTransform.position);  
            }
            else
            {
                _mover.StopMoving();
                StopFollowingCombatTarget(); //optional, I prefer not to have continuous combat in a ARPG.
            }
        }

        public void Attack(CombatTarget target)
        {
            _targetTransform = target.transform;
            _hasCombatTarget = true;
        }

        public void StopFollowingCombatTarget()
        {
            _hasCombatTarget = false;
        }
    }   
}

My InteractWithMovement method in PlayerController.cs that calls StopFollowingCombatTarget():

 bool InteractWithMovement()
        {
            bool hasHit = Physics.Raycast(GetMouseRay(), out RaycastHit hit);
        
            if (hasHit)
            {
                if (Input.GetMouseButton(0))
                {
                    _mover.MoveTo(hit.point);
                    _fighter.StopFollowingCombatTarget(); //this is all
                }
                return true;
            }
            return false;
        }

Not only do you not have to create separation inside the Mover script for no real reason, which is nice but not crucial, the most important part is that you are not doing a null compare in a Update() method which is just, well bad practice (if you don’t need to)? https://github.com/JetBrains/resharper-unity/wiki/Avoid-null-comparisons-against-UnityEngine.Object-subclasses

I am not posting to bash on the code he wrote, I am asking if there is any reason the way he wrote it is better than my way because (subjectively) I think my code is easier to understand and add less noise overall.

I did this course a few months ago and I took my own route in some of the scripts, it did cause some issues because Sam refactors a lot of things later on, so my suggestion would be:

If you are confident enough that you can find a solution regardless of what Sam later does then stick with your solution, keep in mind that this course gets big really quickly, if you think you’ll have issues with this then stick to what Sam and Rick do, or be brave and stay with your solution, if something comes up take it as practice, which is a good thing because in real life you’ll be reading and refactoring code you didn’t write.

I know you probably wanted a more “yes or no” type of answer, but unfortunately is not that simple.

Thanks for the reply. I decided in the end to follow their way of doing things and I will take it as a challenge to go back and refactor everything (if I think there is still a need to) after the course instead.

Later in the course, we’re going to be abstracting interactions even further, and while InteractWithMovement will still be directly in the control of the PlayerController, we’re actually going to be giving control of other elements, including fighter, to the objects that we click on. The TLDR of this is that in the Movement code, it may not even be the fighter we’ll be cancelling.

This isn’t to say your method won’t work, and you could include in figher’s Cancel() method (see two lectures further on) a reference to cancelling the boolean.

This topic was automatically closed after 35 hours. New replies are no longer allowed.

Privacy & Terms