BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Scripts / Grabbable.cs

Grabbable.cs

09143ea9 History
Grabbable.cs 1.2 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 virtual void Grab(ObjectGrabber grabber)
29 {
30 rigidbody.isKinematic = true;
31 rigidbody.constraints = RigidbodyConstraints.FreezeAll;
32
33 transform.parent = grabber.transform;
34
35 grabbedByObject = grabber;
36 }
37
38 public virtual void Release(Vector3 velocity, Vector3 torque)
39 {
40 rigidbody.isKinematic = false;
41 rigidbody.constraints = RigidbodyConstraints.None;
42 rigidbody.velocity = (velocity);
43 rigidbody.AddTorque(torque);
44
45 transform.parent = oldParent;
46 grabbedByObject = null;
47 }
48}