Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Selecting an object by looking at it in an FPS game?

Discussion in 'Scripting' started by derkoi, Aug 1, 2014.

  1. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,255
    Hey guys, I'm trying to select an object in game by looking at it & pressing return to use it.

    For the most part my script is working but I'm having difficulty in deselecting an item when the player looks away from it.

    I'm using raycasts. Selectable game objects have another script attached to them, here's what I have so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class selectionRaycast : MonoBehaviour {
    5.  
    6.     public GameObject itemPanel;
    7.     public UILabel textLabel;
    8.  
    9.     public int raycastLength = 3;
    10.     public bool doLog = false;
    11.     bool firstGameobjectSet = false;
    12.     bool forgetNewObject = false;
    13.     bool processingRaycast = false;
    14.     RaycastHit hitInfo;
    15.     GameObject oldGameObject;
    16.     public GameObject hitObject;
    17.     bool processNewObject = false;
    18.     public string messagePartA = "Press ";
    19.     public string messagePartB = " to Use";
    20.     public Color instructionColour;
    21.     string hexCode;
    22.     selectableObject objectScript;
    23.     selectableObject oldObjectScript;
    24.  
    25.     public bool usingObject = false;
    26.  
    27.     // Use this for initialization
    28.     void Start () {
    29.         itemPanel.SetActive(false);
    30.         hexCode = ColorToHex(instructionColour);
    31.     }
    32.    
    33.     // Update is called once per frame
    34.     void Update ()
    35.     {
    36.  
    37.         if (Physics.Raycast(transform.position, transform.forward, out hitInfo, raycastLength))
    38.         {
    39.             processingRaycast = true;
    40.  
    41.             if (!usingObject)
    42.             {
    43.  
    44.                 hitObject = hitInfo.transform.gameObject;
    45.                 objectScript = hitObject.GetComponent<selectableObject>();
    46.  
    47.                 if (objectScript != null)
    48.                 {
    49.  
    50.                     if (!firstGameobjectSet)
    51.                     {
    52.                         oldGameObject = hitInfo.transform.gameObject;
    53.                         ProcessHitObject();
    54.                         firstGameobjectSet = true;
    55.                        
    56.                     }
    57.  
    58.                     else if (processNewObject || hitInfo.transform.gameObject.GetInstanceID() != oldGameObject.GetInstanceID())
    59.                     {
    60.                         oldGameObject = hitInfo.transform.gameObject;
    61.                         processNewObject = false;
    62.                         ProcessHitObject();
    63.                     }
    64.  
    65.                     else
    66.                     {
    67.                         forgetObject();
    68.                     }
    69.  
    70.                 }
    71.              
    72.                 else
    73.                 {
    74.                 if (doLog) Debug.Log("There was no selectableObject component on the object.");
    75.                 forgetObject();
    76.                 }
    77.  
    78.             }
    79.        
    80.         }
    81.         else
    82.         {
    83.             itemPanel.SetActive(false);
    84.         }
    85.        
    86.     }
    87.  
    88.     void LateUpdate()
    89.     {
    90.         if (processingRaycast)
    91.         {
    92.             if (!Physics.Raycast(transform.position, transform.forward, out hitInfo, raycastLength)) forgetObject();
    93.         }
    94.     }
    95.  
    96.     void ProcessHitObject()
    97.     {
    98.             forgetNewObject = true;
    99.             textLabel.text = objectScript.objectName +"\n"+ "["+hexCode+"]"+ messagePartA + cInput.GetText("Use Item")+ messagePartB;
    100.             itemPanel.SetActive(true);
    101.             objectScript.isSelected = true;
    102.             if (doLog) Debug.Log(objectScript.objectName + " object hit!");
    103.        
    104.     }
    105.  
    106.     string ColorToHex(Color32 color)
    107.     {
    108.         string hex = color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2");
    109.         return hex;
    110.     }
    111.  
    112.     void noObject()
    113.     {
    114.         objectScript.isSelected = false;
    115.     }
    116.  
    117.     void forgetObject()
    118.     {
    119.         if (forgetNewObject)
    120.         {
    121.             if(objectScript != null) objectScript.isSelected = false;
    122.             if (doLog) Debug.Log("Forgetting Object.");
    123.             forgetNewObject = false;
    124.             processingRaycast = false;
    125.         }
    126.     }
    127. }
    Can anyone help please?
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    At a first glance you seem to have a lot of conditionals. You should try to limit the number of booleans you use, since it is mostly confusing. Also, RaycastHits should be declared inside Update(), so that they reset every frame.

    I would do something in the line of:

    Code (csharp):
    1.  
    2. GameObject selected_item;
    3.  
    4. void Update()
    5. {
    6.       RaycastHit hit;
    7.       LayerMask mask;  //give selectable items their own layer
    8.        if(Physics.Raycast(transform.position, transform.forward, out hit, raycastLength, mask))
    9.        {
    10.              selected_item = hit.transform.gameObject;
    11.       }
    12.       else
    13.       {
    14.                selected_item = null;
    15.      }
    16.  
    17.      //show the selected item stats
    18.      if(selected_item != null)
    19.      {
    20.               //do stuff
    21.       }
    22. }
    23.  
     
    Last edited: Aug 1, 2014