Cant get Interact with movement to print

Lecture 27
not sure why but i cannot get the (“cant move”) to print when clicking outiside of the world
everything seems to work, just no console feedback.
i use a MAC (in case that matters)

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


namespace RPG.Control 
{
    public class PlayerController : MonoBehaviour
    {
        
        private void Update()
        {
            if (InteractWithCombat()) return;
            if (InteractWithMovement()); return;
            print("cant move");   //outside navmesh - edge of world
        }


        private bool InteractWithCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue;

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

        private bool InteractWithMovement()
        {
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().MoveTo(hit.point);
                    print("Kiss me i move");
                }
                return true;
            }
            return false;
        }

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

}
1 Like

The code is correct, indicating that the system thinks it’s hitting something… any chance there is physical skybox in the scene?

1 Like

Hi Brian

Thanks for responding.
There is in fact nothing oin the way. The code "interact with movement "seems to function but the debug text does not appear making me think the issue has to do with something being wrong in the Update function. (maybe i need an else? )

i do get a warning in my console.

Assets/Scripts/ControllerScripts/PlayerController.cs(16,40): warning CS0642: Possible mistaken empty statement

which refers to line. if (InteractWithMovement()); return;

1 Like

Yep, that’s the issue. Didn’t notice it on my first pass.
That line is functionally equivalent to

if(InteractWithMovement())
{
}
return;

That “Possible mistaken empty statement” is because as soon as the compiler sees the ;, it assumes that the if block is empty. The code then continues on to the return, which is unconditional.
Remove the ; between the if statement and return and you should be good to go.

1 Like

ha! Thanks for your assistance, works like a charm now.

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

Privacy & Terms