Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Efficient method to create reticle?

Discussion in 'Scripting' started by Hero101, Jul 29, 2015.

  1. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    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.
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
    Kiwasi and Hero101 like this.
  3. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Thank you I greatly appreciate all of the helpful feedback :)
     
  4. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    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.
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
  6. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Thank you for your input I'm sure I can figure it out now.
     
  7. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    I've spent the last 7 hours trying to get this to work... I eventually got it to work with this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ReticleInteraction : MonoBehaviour {
    5.  
    6.  
    7.     Camera camera;
    8.  
    9.  
    10.     void Start ()
    11.     {
    12.         camera = GetComponent<Camera>();
    13.     }
    14.  
    15.     void Update ()
    16.     {
    17.         Ray ray = camera.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    18.         RaycastHit hit;
    19.         if (Physics.Raycast(ray, out hit, 4f))
    20.         {
    21.             Debug.Log (hit.collider.gameObject.name);
    22.         }
    23.     }
    24. }
    25.  
    It works but am I doing it "wrong"?
     
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Nope, that's fine.
     
  9. markasuter

    markasuter

    Joined:
    May 20, 2019
    Posts:
    22
    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!