Configurable cursor issue - core combat

I have completed the configurable cursors section and I am having an odd issue. all of the cursor types work and I have no errors however the cursor doesn’t change to “None” when I am hovering over areas that shouldn’t be moveable to, such as trees, water, or areas that are outside the navmesh boundaries. I replaced my playercontroller.cs with a copy from github to ensure I didn’t have a typo or something like that. I think this may have something to do with the terrain but everything in the editor looks fine. Also if I go to the edge of the terrain the cursor will change to the red X as expected. I have made trees and building static and rebaked the navemesh but those objects are still clickable as iff I can move there, and many times the character will try to run there even though there is no navmesh to run on. any suggestions out there?

I am using Unity 2020.3.30f1
https://github.com/Shakiel777/RPG-Villagers.git

unfortunately, that link doesn’t work, and there is no public repo on your GitHub account named RPG-Villagers (you have an RPG project there, but it’s 3 years old).

Paste in your PlayerController script and we’ll see if we can diagnose this the old fashioned way.

I updated the link for github in the original post, I also included a video that demonstrates the issues. note that the cursor does not change where there is no navmesh, and that the cursor doesn’t change to “none” until the character is out on the docks and you can see the edge of the terrain through the water. this makes me think its an editor setting and not a code issue.

my playercontroller.cs this is one video ahead of where I noted the issue beginning.

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

namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {
        Health health;

        enum CursorType
        {
            None,
            Movement,
            Combat,
            UI,
        }

        [System.Serializable]
        struct CursorMapping
        {
            public CursorType type;
            public Texture2D texture;
            public Vector2 hotspot;
        }

        [SerializeField] CursorMapping[] cursorMappings = null;

        private void Awake()
        {
            health = GetComponent<Health>();
        }

        private void Update()
        {
            if (InteractWithUI()) return;
            if (health.IsDead())
            {
                SetCursor(CursorType.None);
                return;
            }

            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;

            SetCursor(CursorType.None);
        }

        private bool InteractWithUI()
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                SetCursor(CursorType.UI);
                return true;
            }
            return false;
        }

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

                if (!GetComponent<Fighter>().CanAttack(target.gameObject))
                {
                    continue;
                }

                if (Input.GetMouseButton(0))
                {
                    GetComponent<Fighter>().Attack(target.gameObject);
                }
                SetCursor(CursorType.Combat);
                return true;
            }
            return false;
        }

        private bool InteractWithMovement()
        {
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetMouseRay(), out hit);
            if (hasHit)
            {
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().StartMoveAction(hit.point, 1f);
                }
                SetCursor(CursorType.Movement);
                return true;
            }
            return false;
        }

        private void SetCursor(CursorType type)
        {
            CursorMapping mapping = GetCursorMapping(type);
            Cursor.SetCursor(mapping.texture, mapping.hotspot, CursorMode.Auto);
        }

        private CursorMapping GetCursorMapping(CursorType type)
        {
            foreach (CursorMapping mapping in cursorMappings)
            {
                if (mapping.type == type)
                {
                    return mapping;
                }
            }
            return cursorMappings[0];
        }

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

this issue is resolved in the “Raycasting to a Navmesh” just a few sections ahead.

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

Privacy & Terms