Cursor changes only once

Hello everyone!

My delegate gets called when I hover over a new layer, but only once! If I put the cursor back over the ground after it has been over an enemy, the cursor is still a sword.

I’ve noticed while playing around with the camera that if I put an if-statement in Update() it will only run once. Why is that? And could that have to do with it?

Or could it perhaps be some other problem?

Here is the CursorAffordance script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent( typeof(CameraRaycaster))]
public class CursorAffordance : MonoBehaviour {

    [SerializeField] Texture2D attackCursor = null;
    [SerializeField] Texture2D idleCursor = null;
    [SerializeField] Vector2 cursorHotspot = new Vector2(96, 96);

    CameraRaycaster cameraRaycaster;

    // Use this for initialization
    void Start () {
        cameraRaycaster = GetComponent<CameraRaycaster>();
        cameraRaycaster.layerChangeObservers += OnDelegateCalled; // Add to pool in CameraRaycaster
    }
	
	// Update is called once per frame
	void Update ()
    {

    }

    private void OnDelegateCalled()
    {
        print("Layer changed!");
        switch(cameraRaycaster.LayerHit)
        {
            case Layer.Enemy:
                Cursor.SetCursor(attackCursor, cursorHotspot, CursorMode.Auto);
                break;
            case Layer.Object:
                print("Shiny stuff!");
                break;
            default:
                Cursor.SetCursor(idleCursor, cursorHotspot, CursorMode.Auto);
                return;
        }
    }
}

And here is the CameraRaycaster-script:

using UnityEngine;

public class CameraRaycaster : MonoBehaviour
{
    public Layer[] layerPriorities = {
        Layer.Enemy,
        Layer.Object
    };

    float distanceToBackground = 100f;
    Camera viewCamera;

    RaycastHit raycastHit;
    Layer layerHit;

    public RaycastHit Hit
    {
        get { return raycastHit; }
    }

    public Layer LayerHit
    {
        get { return layerHit; }
    }

    public delegate void OnLayerChange(); // Declare new delegate type
    public OnLayerChange layerChangeObservers; // Instantiate an observer pool

    void Start()
    {
        viewCamera = Camera.main;
    }

    void Update()
    {
        // Look for and return priority layer hit
        foreach (Layer layer in layerPriorities)
        {
            var hit = RaycastForLayer(layer);

            if (hit.HasValue)
            {
                raycastHit = hit.Value;

                if (layerHit != layer) // if layer has changed
                {
                    layerHit = layer;
                    layerChangeObservers(); // call the delegates
                }
                layerHit = layer;
                return;
            }
        }

        // Otherwise return background hit
        raycastHit.distance = distanceToBackground;
        layerHit = Layer.RaycastEndStop;
    }

    RaycastHit? RaycastForLayer(Layer layer)
    {
        int layerMask = 1 << (int)layer; // See Unity docs for mask formation
        Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit; // Used as an out parameter
        bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
        if (hasHit)
        {
            return hit;
        }
        return null;
    }
}

I think I solved it… I didn’t assign any layer to the ground, since I’m not implementing click-to-move in my game, and thus the layer didn’t change.

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

Privacy & Terms