Advertisement
halleman19

ray to click | unity3d

Sep 24th, 2023
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class BuildManager : MonoBehaviour
  6. {
  7.     public Camera mainCam;
  8.  
  9.     public GameObject prevObj;
  10.     public GameObject currObj;
  11.  
  12.     private void Update()
  13.     {
  14.         if (Input.GetKeyDown(KeyCode.Mouse0))
  15.         {
  16.             clickToGrid();
  17.         }
  18.     }
  19.  
  20.     private void clickToGrid()
  21.     {
  22.         Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
  23.         Ray ray = mainCam.ScreenPointToRay(pos);
  24.  
  25.         RaycastHit hit;
  26.  
  27.         Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
  28.  
  29.         if (Physics.Raycast(mainCam.ScreenPointToRay(pos), out hit, 10))
  30.         {
  31.             Debug.Log(hit.transform.name);
  32.         }
  33.     }
  34. }
  35.  
Tags: Unity3d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement