Be the first to post for 'Using Raycasts To Query Click'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

Hi guys

Is there any reason why you get components by tag or using the method GetComponentByType<>? Doesn’t it add more overhead at the moment of loading the scene? This is what I have in my Cursor.cs and I reference the raycaster by dragging it in from the editor.

Is there any reason to NOT do that?

Thanks

public class Cursor : MonoBehaviour {

[SerializeField]CameraRaycaster raycaster;

// Use this for initialization
void Start () {
    Assert.IsNotNull(raycaster);
}

// Update is called once per frame
void Update () {
    Debug.Log(raycaster.layerHit);
}

}

Just a note, if you half-follow the video so that you have enemies in your scene, this won’t work yet if you used the same placeholders as in a previous video, since those placeholders don’t have a collider on them. I spent a good 40 seconds trying to figure out why the raycaster didn’t see the enemies :slight_smile: You have to add some kind of collider to test that feature.

2 Likes

DOH! well that explains it. I was trying to figure out as well why the enemies were not seen! :laughing:

Hi Ben,
First of all congratulations for the way you have structured the course and the clear and direct style. Being myself a teacher I really do appreciate the effort.

A small detail concerning your CameraRaycaster.cs script:
Why do not you build a layer mask holding the bits for the two layers and later check the object tag of the hit object?

Privacy & Terms