PlayerPickUp.cs 4.31 KB
Newer Older
Jenny's avatar
Jenny committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPickUp : MonoBehaviour
{
    public Transform player;
    public GameObject ObjectIWantToPickUp;
    public bool hasItem;
    public float ThrowForce;
    public float UpForce;

    public List<GameObject> interactiveobjects;

    public bool canThrow;

Jenny's avatar
Jenny committed
17
18
    private Animator anim;

Jenny's avatar
Jenny committed
19
20
21
22
23
    void Start()
    {
        interactiveobjects = new List<GameObject>();
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;
Jenny's avatar
Jenny committed
24
25

        anim = GetComponentInChildren<Animator>();
Jenny's avatar
Jenny committed
26
27
28
29
30
31
32
    }
    void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            if (!hasItem && interactiveobjects.Count > 0)
            {
Jenny's avatar
Jenny committed
33
34
35
                PlayPickUpAnim();
                //PickUpObject();
                Invoke("PickUpObject", 1);
Jenny's avatar
Jenny committed
36
37
38
39
40
41
42
43
44
45

            }
            else if (hasItem)
            {
                DropObject();
            }
        }

        if (hasItem == true)
        {
Jenny's avatar
Jenny committed
46
47
            anim.SetBool("PickUp", false);

Jenny's avatar
Jenny committed
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
            ObjectIWantToPickUp.GetComponent<Rigidbody>().velocity = Vector3.zero;
            ObjectIWantToPickUp.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;

            if (ObjectIWantToPickUp.tag == "Throwable")
            {
                //Cursor.visible = true;
                //Cursor.lockState = CursorLockMode.Confined;
            }

            if (Input.GetKeyDown("r"))
            {
                if (ObjectIWantToPickUp.tag == "Throwable")
                {
                    ObjectIWantToPickUp.GetComponent<Rigidbody>().useGravity = true;

                    ObjectIWantToPickUp.transform.parent = null;

                    ObjectIWantToPickUp.GetComponent<Rigidbody>().AddForce(player.transform.forward * ThrowForce);
                    ObjectIWantToPickUp.GetComponent<Rigidbody>().AddForce(player.transform.up * UpForce);

                    ObjectIWantToPickUp.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;

                    hasItem = false;
                    
                    Cursor.visible = false;
                    Cursor.lockState = CursorLockMode.Locked;
                }
            }
        }
    }

    private void PickUpObject()
    {
        ObjectIWantToPickUp = GetClosest();
        ObjectIWantToPickUp.GetComponent<Rigidbody>().useGravity = false;
        ObjectIWantToPickUp.transform.parent = player.transform;
        ObjectIWantToPickUp.transform.position = player.position;
        ObjectIWantToPickUp.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        ObjectIWantToPickUp.transform.rotation = player.rotation;

        hasItem = true;

    }

    private void DropObject()
    {
        ObjectIWantToPickUp.GetComponent<Rigidbody>().useGravity = true;

        ObjectIWantToPickUp.transform.parent = null;
        ObjectIWantToPickUp.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
        Cursor.visible = false;

        hasItem = false;
    }

    void FixedUpdate()
    {
        if (hasItem == true)
        {
            ObjectIWantToPickUp.transform.position = player.position;
            ObjectIWantToPickUp.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.layer == 10)
        {
            interactiveobjects.Add(other.gameObject);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (interactiveobjects.Contains(other.gameObject))
        {
            interactiveobjects.Remove(other.gameObject);
Jenny's avatar
Jenny committed
125
            anim.SetBool("PickUp", false);
Jenny's avatar
Jenny committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
        }
    }

    private GameObject GetClosest()
    {
        int s = 0;
        float compare = Mathf.Infinity;
        for (int i = 0; i < interactiveobjects.Count; i++)
        {
            float distance = (interactiveobjects[i].transform.position - transform.position).magnitude;
            if(distance < compare)
            {
                s = i;
                compare = distance;

            }
        }
        return interactiveobjects[s];
    }
Jenny's avatar
Jenny committed
145
146
147
148
149

    private void PlayPickUpAnim()
    {
        anim.SetBool("PickUp", true);
    }
Jenny's avatar
Jenny committed
150
}