Raycast not working

I wasn’t able to get the Raycasting to work. Nothing gets printed to the console. I’m using the FreeLookCameraRig and tried placing the scripts on FreeLookCameraRig, Pivot, and the Main Camera. Enemy is on layer 9, walkable is on layer 19, and the Utility script reads:

public enum Layer
{
    Walkable = 19,
    Enemy = 9,
    RaycastEndStop = -1
}

I have the Invector Melee Combat Template in my project which preset many layers. Enemy was already on 9, and then I set 19. I would think it shouldn’t matter which layer. Anyone else have an issue with this, or anything else to try?

Hi Dan,

Taking the Invector Melee Combat Template out of the equation momentarily, I’d probably start by looking at the code around the delegates. You can use Debug.Log in the methods which are subscribed to the events to see whether those calls are made. If they are not, then I’d be looking at the code which announces to all of the subscribers that the event has just occurred, again, using Debug.Log there also. Probably the place to start.

Just had the same problem, after hour realized a } was missing

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

public class Cursor : MonoBehaviour {

    CameraRaycaster cameraRaycaster;

    // Use this for initialization
    void Start()
    {
        cameraRaycaster = GetComponent<CameraRaycaster>();
    } //was missing......
        // Update is called once per frame
        void Update() {
            print(cameraRaycaster.layerHit);
        }
  }

A tip for when you start out with regards to brackets and braces, consider using a block format instead of trainling, e.g.

Block

public void Start()
{
    if(1 == 1)
    {
        Debug.Log("1 does indeed equal 1");
    }
}

Trailing

public void Start() {
    if(1 == 1) {
        Debug.Log("1 does indeed equal 1");
    }
}

IDEs such as Visual Studio offer tools which will allow you to select one bracket/brace and it will highlight the corresponding one too, this can be very useful, but if you adopt the block approach you’ll start to spot missing braces/braces just by eye as you’ll see that they aren’t indented where you would expect one to be.

It is true that you’ll use a few more lines, but at this stage that really doesn’t matter. When you code is compiled your comments and white space are going to be stripped out anyway, so make the most of these features when starting out to make things easier to follow and understand.

Hope this helps :slight_smile:

Privacy & Terms