Advertisement
Guest User

drag

a guest
Oct 16th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using Sirenix.OdinInspector;
  5. using UnityEngine;
  6.  
  7. public class DragRigidbody : MonoBehaviour
  8. {
  9.  
  10.     public float forceAmount = 3000;
  11.  
  12.     [SerializeField]
  13.     Rigidbody selectedRigidbody;
  14.     [SerializeField]
  15.     Camera targetCamera;
  16.     [SerializeField]
  17.     Vector3 originalScreenTargetPosition;
  18.     [SerializeField]
  19.     Vector3 originalRigidbodyPos;
  20.     [SerializeField]
  21.     float selectionDistance;
  22.  
  23.     // Start is called before the first frame update
  24.     void Start()
  25.     {
  26.         targetCamera = GetComponent<Camera>();
  27.     }
  28.  
  29.     void Update()
  30.     {
  31.         if (!targetCamera)
  32.             return;
  33.  
  34.         if (Input.GetMouseButtonDown(0))
  35.         {
  36.             //Check if we are hovering over Rigidbody, if so, select it
  37.             selectedRigidbody = GetRigidbodyFromMouseClick();
  38.            
  39.         }
  40.         if (Input.GetMouseButtonUp(0) && selectedRigidbody)
  41.         {
  42.             //Release selected Rigidbody if there any
  43.             selectedRigidbody = null;
  44.         }
  45.     }
  46.  
  47.     void FixedUpdate()
  48.     {
  49.         if (selectedRigidbody)
  50.         {
  51.             Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance)) - originalScreenTargetPosition + Vector3.up * 2;
  52.             // Vector3 mousePositionOffset = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0, Input.mousePosition.y)) - originalScreenTargetPosition + Vector3.up * 2;
  53.             Vector3 velocityVector =
  54.                 (originalRigidbodyPos + mousePositionOffset - selectedRigidbody.transform.position) * forceAmount *
  55.                 Time.deltaTime;
  56.             selectedRigidbody.velocity = new Vector3(velocityVector.x, 0,velocityVector.z * 6f);
  57.         }
  58.     }
  59.  
  60.     Rigidbody GetRigidbodyFromMouseClick()
  61.     {
  62.         RaycastHit hitInfo = new RaycastHit();
  63.         Ray ray = targetCamera.ScreenPointToRay(Input.mousePosition);
  64.        
  65.         bool hit = Physics.Raycast(ray, out hitInfo);
  66.         if (hit)
  67.         {
  68.             if (hitInfo.collider.gameObject.GetComponent<Rigidbody>())
  69.             {
  70.                 selectionDistance = Vector3.Distance(ray.origin, hitInfo.point);
  71.                 originalScreenTargetPosition = targetCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, selectionDistance));
  72.                 originalRigidbodyPos = hitInfo.collider.transform.position;
  73.                
  74.                 hitInfo.collider.gameObject.transform.DOMove( hitInfo.collider.gameObject.transform.position + Vector3.up
  75.                     , 0.2f);
  76.                
  77.                 return hitInfo.collider.gameObject.GetComponent<Rigidbody>();
  78.             }
  79.         }
  80.  
  81.         return null;
  82.     }
  83. }
  84.  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement