RPG Raycast mouse

Hello, in the RPG Core Combat Creator - Archived Course , on the first Section of the ‘10. Using Raycasts To Query Click’, even do the scripts are the same of the teacher when i pass the mouse on the blocks or enemys it keeps saying RaycastEndStop


CameraRaycaster.cs

using UnityEngine;

public class CameraRaycaster : MonoBehaviour
{
    public Layer[] layerPriorities = {
        Layer.Enemy,
        Layer.Walkable
    };
    [SerializeField] float distanceToBackground = 100f;

    Camera viewCamera;
    RaycastHit m_hit;

    public RaycastHit hit
    {
        get { return m_hit; }
    }

    Layer m_layerHit;
    public Layer layerHit
    {
        get { return m_layerHit; }
    }

    void Start() // TODO Awake?
    {
        viewCamera = Camera.main;
    }

    void Update()
    {
        // Look for and return priority layer hit
        foreach (Layer layer in layerPriorities)
        {
           var hit = RaycastForLayer(layer);
            if (hit.HasValue)
            {
                m_hit = hit.Value;
                m_layerHit = layer;
                return;
            }
        }

        // Otherwise return background hit
        m_hit.distance = distanceToBackground;
        m_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;
    }
}

Cursor.cs

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

public class Cursor : MonoBehaviour {

    CameraRaycaster cameraRaycaster;

    // Use this for initialization
    void Start () {
        cameraRaycaster = GetComponent<CameraRaycaster>();
    }

    // Update is called once per frame

    void Update () {
        print(cameraRaycaster.layerHit);
    }
}

Utility.cs

public enum Layer{ // crei una lista di
    Walkable = 8,
    Enemy = 9,
    RaycastEndStop = -1
}

It’s been quite a while since I’ve seen anything of the archived course.

So looking over the code, I can only think of a couple things that might be causing the issue:

  • Make sure that the enemies and ground have their layers properly set to Enemy, Walkable, etc
  • Check to make sure that the distanceToBackground has not been serialized in the inspector to a lower value than the default 100m
  • Try increasing the distanceToBackground. Remember that if you’ve edited it in the inspector, the inspector’s value always wins.

The layers are properly set, and the the distanceToBackground was 100, i even tried higher values like 1000 and 10000 but it doesn’t work, when i click once i noticed that it keeps walking on the x axes, no matter where i click.




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

Privacy & Terms