Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. class XXX : MonoBehaviour
  2. {
  3.     Plane plane;
  4.     Transform target;
  5.  
  6.     void Update()
  7.     {
  8.         if (Input.GetMouseButtonDown(0))
  9.         {
  10.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  11.  
  12.             if (Physics.Raycast(ray, out RaycastHit hit))
  13.             {
  14.                 target = hit.transform;
  15.  
  16.                 plane = new Plane(-ray.direction, target.position);
  17.             }
  18.         }
  19.         else if (target != null && Input.GetMouseButton(0))
  20.         {
  21.             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  22.  
  23.             if (plane.Raycast(ray, out float enter))
  24.             {
  25.                 Vector3 hitPoint = ray.GetPoint(enter);
  26.  
  27.                 target.position = hitPoint;
  28.             }
  29.         }
  30.         else if (Input.GetMouseButtonUp(0))
  31.         {
  32.             target = null;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement