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

Casting Ray from the Mouse every frame. Please Help!

Discussion in 'Community Learning & Teaching' started by Red_Kay, Aug 27, 2015.

  1. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    Greetings,

    So, I want to cast a ray from the mouse everyframe and get the "gameObject" that the ray is hitting.
    I have heard that using Input.mousePosition returns a Vector3 variable so I used that for it. I have checked the unity manual on casting ray from mouse but didnt understand what this statement mean:

    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Can anyone explain this thoroughly please :)

    This is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickDetection : MonoBehaviour {
    5.  
    6.     public GameObject cube;
    7.  
    8.     ClickDetection script;
    9.     Quaternion target;
    10.     int count = 0;
    11.     Ray ray;
    12.     Vector3 mouseLoc;
    13.     RaycastHit hit;
    14.     Transform cardTransform;
    15. //    int cardCode;
    16.    
    17.     void Start()
    18.     {
    19.         target = new Quaternion (transform.rotation.x , -180 , transform.rotation.z , 0.0f);
    20.         script = GetComponent <ClickDetection> ();
    21.     }
    22.    
    23.     void Update()
    24.     {
    25.         mouseLoc = Input.mousePosition;
    26.         ray = new Ray (mouseLoc, Vector3.forward);
    27.         //Physics.Raycast (ray , out hit  );
    28.         //Debug.DrawLine (mouseLoc , new Vector3 (mouseLoc.x , mouseLoc.y , 100));
    29.         Debug.Log ("  "+mouseLoc);
    30.         //cardHit = hit.rigidbody;
    31.         if (Physics.Raycast (ray, out hit)) {
    32.        
    33.             if (hit.collider.gameObject.tag == "Card") {
    34.                 Debug.Log ("Got something!");
    35.             } else {
    36.                 Debug.Log ("Error");
    37.             }
    38.        
    39.         }
    40.         if (Input.GetMouseButton(0))
    41.         {
    42.             cardTransform.rotation =  Quaternion.Slerp(cardTransform.rotation , target , 20.5f * Time.deltaTime);
    43.             hit.rigidbody.gameObject.transform.rotation = cardTransform.rotation;
    44.             count++;
    45.             if (count == 2) {
    46.                
    47.                 script.enabled = false;
    48.             }
    49.         }
    50.     }
    51.  
    52. }
    53.  
    I know few statements are wrong but that is because they are incomplete and I am still working on the logic :)
    Just help me with the mouse raycasting stuff!
     
  2. DenisLemos

    DenisLemos

    Joined:
    May 1, 2015
    Posts:
    781

    This creates an infinite line that starts from the position where the mouse is on the screen, and goes in the direction the camera is pointing.
    To find the object that this ray is hitting, you also need to create a RaycastHit that will get the raycast informations.
    And check if this ray has intersected any collider using Physics.Raycast
    Exemple:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RayTest : MonoBehaviour
    5. {
    6.     // Update is called once per frame
    7.     void Update ()
    8.     {
    9.         RaycastHit hit; // To get raycast information
    10.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Create the ray from screen to infinite
    11.         if (Physics.Raycast(ray, out hit)) // If has intersected a collider
    12.         {
    13.             GameObject getObject = hit.collider.gameObject; // Get GameObject that owns this collider
    14.             Debug.Log(getObject.name);
    15.         }
    16.     }
    17. }
    I can not explain very well, because my English is bad, maybe someone can explain it better.
     
    IanSmellis and Red_Kay like this.
  3. Red_Kay

    Red_Kay

    Joined:
    Aug 14, 2015
    Posts:
    94
    Thank you so much I understood it! :)