Cancelling is sending the wrong messages

I compared my code with the github line for line, and it’s identical, and somehow the cancelling function is spewing nothing but falsehoods.
On first play, if I clicked immediately on the target, it cancels Fighting. Clicking away cancels nothing, and the clicking back on the target cancels both fighting AND Mover.
image
And this makes zero sense to me. Logically, if my code is exactly what the instructor is using, it should do the same thing. But, maybe there’s something weird in another script? I have no idea.

Paste in your ActionScheduler.cs script and we’ll have a look.

namespace RPG.Core
{
public class ActionScheduler : MonoBehaviour
{
    MonoBehaviour currentAction;
    public void StartAction(MonoBehaviour action)
    {
        if (currentAction == action) return;
        if (currentAction != null)
        {
            print("Cancelling" + currentAction); 
        }
            currentAction = action;
    }

}
}

What’s wrong with him, doc? :C

So it looks like what might be happening is that the click on InteractWithCombat is being immediately followed by a positive hit on InteractWithMovement. Usually, this is caused when clicking on a collider and then before the mouse can be released it being no longer on the collider (like when your camera moves).

There are a couple of solutions… One is to make sure that the enemy’s collider fully covers the character. Sometimes, it’s even a good idea to make it a bit larger to ensure you catch the enemy.
The other is to change the code in PlayerController.InteractWithMovement, instead of using GetMouseButton(0), use GetMouseButtonDown(0). This means you can’t do click and hold movement, but it eliminates this Fighter=>Mover on a single click issue very effectively.

Any changes that were supposed to happen on PlayerController.cs are completely missing from this lesson’s github and I haven’t the foggiest idea as to why. I can’t compare my current code with whatever changes were almost certainly made without going back through two videos, and that’s… mildly inconvenient.

Also, unfortunately, the changes you suggested didn’t fix anything. I’m getting the feeling is actually with my PlayerController script, but I can’t tell what I borked in it.

using UnityEngine;
using RPG.Movement;
using RPG.Combat;
using System;

namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {
        void Update()
        {
            InteractWithCombat();
            InteractWithMovement();
        }


        private void MoveToCursor()
        {
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                GetComponent<Mover>().StartMoveAction(hit.point);
            }
        }

        private static Ray GetMouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }

        private void InteractWithMovement()
        {
            if (Input.GetMouseButton(0))
            {
                MoveToCursor();
            }
        }

        private void InteractWithCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue; //continue means to carry on with the loop and go to next object.

                if(Input.GetMouseButtonDown(0))
                {
                    GetComponent<Fighter>().Attack(target);
                }
            }
        }

    }
}
type or paste code here

I see it straight away…
You need to quit Update if you can attack.

    private bool InteractWithCombat()
    {
        RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
        foreach (RaycastHit hit in hits)
        {
            CombatTarget target = hit.transform.GetComponent<CombatTarget>();
            if (target == null) continue; //continue means to carry on with the loop and go to next object.

            if(Input.GetMouseButtonDown(0))
            {
                GetComponent<Fighter>().Attack(target);
            }
            return true;
        }
        return false;
    }

Then your Update should say

 if(InteractWithCombat()) return;

image
That seems to have doubled the issue in a most interesting way and now I am only more confused as to what is going wrong.

What baffles me more is that my code really should logically do exactly what it does in the instructional video, but it doesn’t, and no amount of matching the Github changes has fixed this. Either the instructor wrote code that I missed, or wrote code that wasn’t in the video, and I’m really not happy that fixing this is taking… days. I’m going to try and move on and hope that it doesn’t break things further, I guess.

I got a better idea. I’m just gonna check the previous lesson. Something went terribly wrong there or I missed it.

Edit: Yep! Skipped an entirely chunk of the lesson in a moment of insanity somehow. Late spring heat must be getting to me.

I was wondering if a lesson might have been missed. Glad you got it sorted.

Well… I didn’t miss a lesson exactly. The code was in the previous lesson’s github, but never discussed in the video or this lesson I had trouble with. I never actually see all the ‘return true/return false’ lines in the previous lesson, where apparently it was supposed to be written. I may also just be having another moment of insanity, but I am pretty sure it was just not discussed.

Either way, it’s fixed now.

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

Privacy & Terms