| 1 | using System.Collections; |
| 2 | using System.Collections.Generic; |
| 3 | using UnityEngine; |
| 4 | |
| 5 | [RequireComponent(typeof(Rigidbody))] |
| 6 | public class Grabbable : MonoBehaviour |
| 7 | { |
| 8 | private ObjectGrabber grabbedByObject; |
| 9 | [HideInInspector] public Rigidbody rigidbody; |
| 10 | private Transform oldParent; |
| 11 | |
| 12 | // Start is called before the first frame update |
| 13 | void Awake() |
| 14 | { |
| 15 | rigidbody = GetComponent<Rigidbody>(); |
| 16 | oldParent = transform.parent; |
| 17 | } |
| 18 | |
| 19 | public virtual void Grab(ObjectGrabber grabber) |
| 20 | { |
| 21 | rigidbody.isKinematic = true; |
| 22 | rigidbody.constraints = RigidbodyConstraints.FreezeAll; |
| 23 | |
| 24 | transform.parent = grabber.transform; |
| 25 | } |
| 26 | |
| 27 | public virtual void Release(Vector3 velocity, Vector3 torque) |
| 28 | { |
| 29 | rigidbody.isKinematic = false; |
| 30 | rigidbody.constraints = RigidbodyConstraints.None; |
| 31 | rigidbody.velocity = (velocity); |
| 32 | rigidbody.AddTorque(torque); |
| 33 | |
| 34 | transform.parent = oldParent; |
| 35 | } |
| 36 | } |