Efficient method to create reticle?

I’m making a simple first person exploration type game. I want to add a simple white dot reticle to the center of the player’s view. There will be objects that the player can interact with if the reticle is on it.

I’m wondering two things:

  1. What is the most efficient way to implement the “cross hair”? Would it be drawing it with code or to add a tiny sphere to the scene and position it with code? I would prefer to do the latter since I have no idea how to draw with code.

  2. In terms of being able to interact with anything in the line of sight of the reticle: Would that just be a simple raycast that points in the direction that the camera is facing? From what I can remember you can decide the length of a raycast which would help to make sure the player can’t interact with something in the reticle that is far away.

Just trying to figure out which method is best for making a reticle. I’m not attaching it the mouse cursor as my game has that locked and invisible. Since it’s a first-person game where the player is the camera I’m assuming I just have to center the reticle to the center of the screen.

1: The reticle would be drawn in an external program like Photoshop and imported as a sprite/texture. You can put it in a Canvas as an Image, position it properly by just setting it as “centered” and “.5” for X and Y axis alignments. Since it wouldn’t have to move in an FPS game where you can’t move the cursor around, that’s all- done!

2: Just divide the screen height and width by two to give you the “origin point” in screen coordinates (dead center), then use Camera.ScreenToWorldPoint() and raycast from there (into the direction of “camera position forward”). You can indeed set the maximum distance, and then all of your selections can be handled as RaycastHits.

2 Likes

Thank you I greatly appreciate all of the helpful feedback :slight_smile:

Physics.Raycast requires a Vector3 for the origin. I understanding dividing the screen’s height and width by two to get the center point but what about the final part of the vector 3? Or can I just put a Vector2 in the origin argument for the Physics.Raycast?

Sorry having trouble figuring out how to set this all up.

You can use a Vector2 in most places that you use a Vector3 and it’ll work fine usually, but you can also just “Camera.main.ScreenPointToRay(someVector2)” to return a ray pointing in the right direction and use that for your raycast as the first parameter. It’ll cut down on the work needed here.

Thank you for your input I’m sure I can figure it out now.

I’ve spent the last 7 hours trying to get this to work… I eventually got it to work with this:

using UnityEngine;
using System.Collections;

public class ReticleInteraction : MonoBehaviour {


    Camera camera;


    void Start ()
    {
        camera = GetComponent<Camera>();
    }

    void Update ()
    {
        Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, 4f))
        {
            Debug.Log (hit.collider.gameObject.name);
        }
    }
}

It works but am I doing it “wrong”?

Nope, that’s fine.

This works great in 2020.3 LTS. For newbs like me:

  • Attach the above script to the camera (I am using the Unity First Person Controller Asset Package from the Asset Store)
  • the 4 on line 19 is how far away it will register the Raycast hitting something, adjust as needed.

Thanks to both of you!