Advertisement
uurha

Untitled

May 6th, 2022
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Test : MonoBehaviour
  6. {
  7.     private Ray _ray;
  8.     private RaycastHit _hit;
  9.  
  10.     void Update()
  11.     {
  12.         if (Input.GetMouseButton(0))
  13.         {
  14.             Cast();
  15.         }
  16.     }
  17.  
  18.     private void Cast()
  19.     {
  20.         var mouse = Input.mousePosition;
  21.         mouse.z = Camera.main.nearClipPlane;
  22.         _ray = Camera.main.ScreenPointToRay(mouse);
  23.         if (Physics.Raycast(_ray, out _hit, 200))
  24.         {
  25.             Debug.Log(_hit.transform.name);
  26.         }
  27.     }
  28.  
  29.     private void OnDrawGizmos()
  30.     {
  31.         Gizmos.color = Color.red;
  32.         Gizmos.DrawRay(_ray.origin, _ray.direction * 200);
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement