BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Scripts / Grabbable.cs

Grabbable.cs

92128f09 History
Grabbable.cs 1.3 KiB Download Raw
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4
5[RequireComponent(typeof(Rigidbody))]
6public class Grabbable : MonoBehaviour
7{
8 private ObjectGrabber grabbedByObject;
9 [HideInInspector] public Rigidbody rigidbody;
10 private Transform oldParent;
11 public bool Locked { get; set; }
12
13 public bool IsGrabbed
14 {
15 get
16 {
17 return grabbedByObject != null;
18 }
19 }
20
21 // Start is called before the first frame update
22 void Awake()
23 {
24 rigidbody = GetComponent<Rigidbody>();
25 oldParent = transform.parent;
26 }
27
28 public void FreezeRigidbody()
29 {
30 rigidbody.isKinematic = true;
31 rigidbody.constraints = RigidbodyConstraints.FreezeAll;
32 }
33
34 public void UnfreezeRigidbody()
35 {
36 rigidbody.isKinematic = false;
37 rigidbody.constraints = RigidbodyConstraints.None;
38 }
39
40 public virtual void Grab(ObjectGrabber grabber)
41 {
42 FreezeRigidbody();
43
44 transform.parent = grabber.transform;
45
46 grabbedByObject = grabber;
47 }
48
49 public virtual void Release(Vector3 velocity, Vector3 torque)
50 {
51 UnfreezeRigidbody();
52 rigidbody.velocity = (velocity);
53 rigidbody.AddTorque(torque);
54
55 transform.parent = oldParent;
56 grabbedByObject = null;
57 }
58}