-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrabController.cs
More file actions
102 lines (89 loc) · 3.75 KB
/
GrabController.cs
File metadata and controls
102 lines (89 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GrabController : MonoBehaviour {
public GameObject handObject;
public float detectionRadius;
private bool hasObject = false;
private bool inPlace = false;
private Collider2D[] arounds;
private GameObject grabbedObject;
private AudioManager audioManagerScript;
private GameController gameControllerScript;
private void Awake() {
gameControllerScript = FindObjectOfType<GameController>();
audioManagerScript = FindObjectOfType<AudioManager>();
}
private void OnDrawGizmos() {
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(this.transform.position, detectionRadius);
}
private void Update() {
CheckAround();
if (Input.GetMouseButtonDown(0) && !hasObject)
Grab();
if (Input.GetMouseButtonUp(0) && hasObject && inPlace)
stickObject();
else if (Input.GetMouseButtonUp(0) && hasObject)
Drop();
}
private void CheckAround() {
arounds =
Physics2D.OverlapCircleAll(transform.position, detectionRadius);
}
private void Grab() {
if (arounds != null) {
for (int i = 0; i < arounds.Length && !hasObject; i++) {
Debug.Log(i);
if (!hasObject && arounds[i].gameObject.tag == "Caja") {
Debug.Log("picking obj");
//pillar objeto
grabbedObject = arounds[i].gameObject;
takeOutPhysics(grabbedObject);
grabbedObject.transform.position = handObject.transform.position;
grabbedObject.transform.parent = handObject.transform;
hasObject = true;
audioManagerScript.PlayCatch();
}
}
}
}
public void Drop() {
if (hasObject) {
grabbedObject.transform.parent = null;
returnPhysics(grabbedObject);
grabbedObject = null;
hasObject = false;
}
}
private void takeOutPhysics(GameObject pickup) {
pickup.GetComponent<Rigidbody2D>().simulated = false;
pickup.GetComponent<BoxCollider2D>().enabled = false;
}
private void returnPhysics(GameObject pickup) {
pickup.GetComponent<Rigidbody2D>().simulated = true;
pickup.GetComponent<BoxCollider2D>().enabled = true;
}
private void OnTriggerEnter2D(Collider2D collision) {
if (hasObject) {
Debug.Log("Una cosa" + collision.gameObject + " y lo otro " + grabbedObject.GetComponent<ObjectController>().originalTransform);
if (collision.gameObject.tag == "Zone" && hasObject && collision.gameObject == grabbedObject.GetComponent<ObjectController>().originalTransform) {
inPlace = true;
}
}
}
private void OnTriggerExit2D(Collider2D collision) {
inPlace = false;
}
private void stickObject() {
takeOutPhysics(grabbedObject);
Quaternion originalRotation = grabbedObject.GetComponent<ObjectController>().getOriginalRotation();
Transform originalTransform = grabbedObject.GetComponent<ObjectController>().getOriginalTransform();
grabbedObject.transform.rotation = Quaternion.Slerp(grabbedObject.transform.rotation, originalRotation, Time.deltaTime * 180f);
grabbedObject.transform.position = originalTransform.position;
grabbedObject.transform.parent = null;
grabbedObject.GetComponent<ObjectController>().placed = true;
this.gameControllerScript.ObjectPlaced();
this.hasObject = false;
}
}