-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrabable.cs
More file actions
67 lines (59 loc) · 1.6 KB
/
Copy pathGrabable.cs
File metadata and controls
67 lines (59 loc) · 1.6 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody), typeof(BoxCollider))]
public class Grabable : MonoBehaviour
{
[SerializeField] Player player;
private Rigidbody _rigidbody;
private bool canGrab;
private void Start()
{
_rigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
if (player.isGrabbing)
{
Push();
if (Input.GetKeyDown(KeyCode.E))
LetGo();
}
else if (canGrab && Input.GetKeyDown(KeyCode.E))
{
UIManager.instance.TypeInteract("Press E to let go.");
player.isGrabbing = true;
_rigidbody.useGravity = false;
canGrab = false;
}
}
private void Push()
{
if (transform.position.y <= 0.125f)
transform.position = Vector3.Lerp(transform.position, player.transform.position + player.transform.forward * 1.2f, 10f * Time.deltaTime);
else
transform.position = new Vector3(transform.position.x, 0.125f, transform.position.z);
}
private void LetGo()
{
UIManager.instance.HideInteract();
player.isGrabbing = false;
_rigidbody.useGravity = true;
_rigidbody.velocity = Vector3.zero;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (!player.isGrabbing)
UIManager.instance.ShowInteract();
canGrab = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player") && !player.isGrabbing)
UIManager.instance.HideInteract();
canGrab = false;
}
}