Advertisement
Guest User

Pickup

a guest
Jul 22nd, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.15 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class Pickup : MonoBehaviour
  4. {
  5.     private const float MaxDistance = 100f;
  6.     [SerializeField] private float maxScale = 10f;
  7.     [SerializeField] private LayerMask pickable;
  8.     [SerializeField] private LayerMask pickableScalable;
  9.     [SerializeField] private Transform origin;
  10.  
  11.     private Transform target;
  12.     private Rigidbody targetRigidbody;
  13.     private Vector3 startScale;
  14.     private float startDistance;
  15.     private Vector3 targetScale;
  16.     private Vector3 bounds;
  17.     private float circumradius;
  18.  
  19.     private bool IsScaleTooSmall => (targetScale * maxScale).x < startScale.x;
  20.     private bool IsScaleTooBig => (targetScale / maxScale).x > startScale.x;
  21.  
  22.     private void Update()
  23.     {
  24.         HandleInput();
  25.         ResizeObject();
  26.     }
  27.  
  28.     private void HandleInput()
  29.     {
  30.         if (Input.GetMouseButtonDown(0))
  31.         {
  32.             if (target == null)
  33.             {
  34.                 if (Physics.Raycast(origin.transform.position, origin.transform.forward, out var hit, MaxDistance, pickable))
  35.                 {
  36.                     target = hit.transform;
  37.                     targetRigidbody = hit.rigidbody;
  38.                     targetRigidbody.isKinematic = true;
  39.                     bounds = hit.collider.bounds.size;
  40.                     circumradius = Mathf.Max(bounds.x, bounds.y, bounds.z);
  41.  
  42.                     startDistance = Vector3.Distance(origin.transform.position, target.position);
  43.                     startScale = target.transform.localScale;
  44.                     targetScale = target.transform.localScale;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 CancelDrag();
  50.             }
  51.         }
  52.     }
  53.  
  54.     private void ResizeObject()
  55.     {
  56.         if (target == null)
  57.         {
  58.             return;
  59.         }
  60.  
  61.         if (Physics.Raycast(origin.transform.position, origin.transform.forward, out var hit, MaxDistance, ~pickable))
  62.         {
  63.             target.position = hit.point - targetScale.x * origin.transform.forward;
  64.  
  65.             if (Physics.SphereCast(origin.transform.position, circumradius, origin.transform.forward, out hit, MaxDistance, ~pickable))
  66.             {
  67.                 target.position = hit.point + hit.normal * circumradius;
  68.             }
  69.  
  70.             float currentDistance = Vector3.Distance(origin.transform.position, target.position);
  71.             float scale = currentDistance / startDistance;
  72.             targetScale.x = targetScale.y = targetScale.z = scale;
  73.  
  74.             // TODO: move MIN/MAX scale check to drag cancel
  75.             if (IsScaleTooBig)
  76.             {
  77.                 target.localScale = Vector3.one;
  78.                 CancelDrag();
  79.             }
  80.             else
  81.             {
  82.                 if (IsScaleTooSmall)
  83.                 {
  84.                     target.localScale = Vector3.one;
  85.                     CancelDrag();
  86.                 }
  87.                 else
  88.                 {
  89.                     target.localScale = targetScale * startScale.x;
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     private void CancelDrag()
  96.     {
  97.         targetRigidbody.isKinematic = false;
  98.         target = null;
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement