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

Very Dumb Question About Selecting in Unity

Discussion in 'Daydream' started by jobieadobe, May 14, 2018.

  1. jobieadobe

    jobieadobe

    Joined:
    Feb 20, 2013
    Posts:
    1
    Hello Everyone. I'm sorry in advance for how dumb this question is but here goes...

    How do I select an object in game?

    Told you it was dumb. I'm sorry if someone's already asked this I just can't seem to find an answer

    So lets say I hit an object with a event trigger and mesh collider with a physics raycaster. How do I select it? I have been successful in making a unit move with a controller but that's just one unit. I want to be able to look at another unit for a second and it become the selected object and be able to move it and not the other unit. This has to be easy and I'm sure I'm just being daft.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    raycasthit returns all the info from ray hit,
    https://docs.unity3d.com/ScriptReference/RaycastHit.html

    so to print out target name, something like
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0)) {
    2.             RaycastHit hit;
    3.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.             if (Physics.Raycast(ray, out hit)){
    5.                   Debug.Log("we hit "+hit.transform.name);
    6.                   // here you could also set your selectedGameObject = hit.gameObject;
    7.             }
    8.            
    9.         }
    https://docs.unity3d.com/ScriptReference/RaycastHit-collider.html

    or even better, complete one or two of the tutorials, they should probably cover many common cases
    https://unity3d.com/learn/tutorials