-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractiveobjects.cs
More file actions
318 lines (269 loc) · 8.53 KB
/
interactiveobjects.cs
File metadata and controls
318 lines (269 loc) · 8.53 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// ObjectsToCollect.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectsToCollect : MonoBehaviour
{
public static int objects = 0;
void Awake()
{
objects++;
}
void OnTriggerEnter(Collider plyr)
{
if (plyr.CompareTag("Player")) // Updated for better tag comparison
{
objects--;
gameObject.SetActive(false);
}
}
}
// PickUp.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
private Animator anim;
private Inventory invScript;
public bool money;
public int moneyAmount;
private Currency moneyScript;
public bool item;
public GameObject itemIcon;
private bool pickedUp = false;
void Start()
{
var player = GameObject.FindWithTag("Player");
if (player != null)
{
anim = player.GetComponent<Animator>();
}
var gameController = GameObject.FindWithTag("GameController");
if (gameController != null)
{
moneyScript = gameController.GetComponent<Currency>();
invScript = gameController.GetComponent<Inventory>();
}
}
void OnTriggerStay(Collider player)
{
if (player.CompareTag("Player") && Input.GetKeyDown(KeyCode.E) && !pickedUp)
{
pickedUp = true;
StartCoroutine(PlayAnim());
}
}
private IEnumerator PlayAnim()
{
if (anim != null)
{
anim.SetTrigger("pickup");
}
yield return new WaitForSeconds(1);
if (money && moneyScript != null)
{
moneyScript.AddGold(moneyAmount);
Destroy(gameObject);
}
else if (item && invScript != null)
{
GameObject i = Instantiate(itemIcon);
i.transform.SetParent(invScript.invTab.transform, false);
Destroy(gameObject);
}
}
}
// ThrowObject.cs
using UnityEngine;
using System.Collections;
public class ThrowObject : MonoBehaviour
{
public Transform player;
public Transform playerCam;
public float throwForce = 10f;
private bool hasPlayer = false;
private bool beingCarried = false;
public AudioClip[] soundToPlay;
private AudioSource audioSource;
public int dmg;
private bool touched = false;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void Update()
{
float dist = Vector3.Distance(transform.position, player.position);
hasPlayer = dist <= 2.5f;
if (hasPlayer && Input.GetButtonDown("Use"))
{
PickupObject();
}
if (beingCarried)
{
if (touched)
{
DropObject();
touched = false;
}
if (Input.GetMouseButtonDown(0)) // Throw
{
ThrowObjectAction();
}
else if (Input.GetMouseButtonDown(1)) // Drop
{
DropObject();
}
}
}
private void PickupObject()
{
var rb = GetComponent<Rigidbody>();
if (rb != null)
{
rb.isKinematic = true;
}
transform.SetParent(playerCam);
beingCarried = true;
}
private void DropObject()
{
var rb = GetComponent<Rigidbody>();
if (rb != null)
{
rb.isKinematic = false;
}
transform.SetParent(null);
beingCarried = false;
}
private void ThrowObjectAction()
{
DropObject();
var rb = GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddForce(playerCam.forward * throwForce, ForceMode.VelocityChange);
}
PlayRandomAudio();
}
private void PlayRandomAudio()
{
if (audioSource.isPlaying) return;
audioSource.clip = soundToPlay[Random.Range(0, soundToPlay.Length)];
audioSource.Play();
}
void OnTriggerEnter(Collider other)
{
if (beingCarried)
{
touched = true;
}
}
}
// Currency.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Currency : MonoBehaviour
{
public int gold;
private GameObject currencyUI;
void Start()
{
currencyUI = GameObject.Find("Currency");
UpdateCurrencyUI();
}
public void AddGold(int amount)
{
gold = Mathf.Max(0, gold + amount);
UpdateCurrencyUI();
}
private void UpdateCurrencyUI()
{
if (currencyUI != null)
{
var textComponent = currencyUI.GetComponent<Text>();
if (textComponent != null)
{
textComponent.text = gold.ToString();
}
}
}
}
// PickupMoney.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupMoney : MonoBehaviour
{
private Currency currencyScript;
public int addAmount;
void Start()
{
var gameController = GameObject.FindWithTag("GameController");
if (gameController != null)
{
currencyScript = gameController.GetComponent<Currency>();
}
}
void OnTriggerEnter(Collider obj)
{
if (obj.CompareTag("Player") && currencyScript != null)
{
currencyScript.AddGold(addAmount);
Destroy(gameObject);
}
}
}
/*
### Tutorial: How to Implement and Use These Scripts in Unity ###
#### Step 1: Set Up Your Unity Project
1. Open Unity and create a new 3D project.
2. Import your assets (e.g., 3D models for objects, player, and UI icons).
#### Step 2: Add Scripts to GameObjects
- **ObjectsToCollect.cs**: Attach this script to any collectible object in your scene.
- **PickUp.cs**: Attach this script to objects the player can pick up, such as coins or items.
- **ThrowObject.cs**: Attach this script to objects the player can throw.
- **Inventory.cs**: Attach this script to a GameObject managing the inventory system.
- **UseInventory.cs**: Attach this script to consumable or usable item prefabs.
- **Currency.cs**: Attach this script to the GameController to manage currency.
- **PickupMoney.cs**: Attach this script to money objects that can be picked up.
#### Step 3: Configure GameObjects
1. **Objects to Collect**:
- Add a Collider (e.g., Sphere Collider) and set it as a Trigger.
- Tag the object with a suitable tag (e.g., "Collectible").
2. **Player Setup**:
- Ensure your player GameObject has the "Player" tag.
- Add an Animator component and set up animations for picking up objects.
- Attach the `ThirdPersonCharacter` script to your player GameObject and configure health, hunger, and thirst.
3. **GameController**:
- Create an empty GameObject and tag it as "GameController".
- Add scripts for managing inventory (`Inventory.cs`) and consumables (`UseInventory.cs`).
- Set up the `invTab` property in the `Inventory.cs` script to reference your UI inventory panel.
4. **Pickable Objects**:
- Attach the `PickUp` script to objects.
- Configure the `money`, `moneyAmount`, `item`, and `itemIcon` properties in the Inspector.
5. **Money Objects**:
- Attach the `PickupMoney` script to money objects.
- Set the `addAmount` property in the Inspector to define how much gold the object adds.
6. **UI Setup**:
- Create sliders for health, hunger, and thirst, and link them to the respective properties in the `ThirdPersonCharacter` script.
- Add buttons or interactive elements to use items and call `UseItem` in the `UseInventory` script.
- Add a UI Text element named "Currency" to display the player's gold amount.
#### Step 4: Input Configuration
- Ensure that input mappings for "Use", "Tab", and mouse buttons are set in Unity's Input Manager.
- "Use": Map it to a key like "E".
- "Tab": Map it for toggling inventory.
#### Step 5: Test Your Game
1. Play your scene.
2. Move the player near objects and test collecting, picking up, and throwing objects.
3. Toggle the inventory using the `Tab` key and interact with items.
4. Observe sliders for health, hunger, and thirst values, and verify their changes during gameplay.
5. Collect money objects and confirm that the currency UI updates correctly.
#### Notes:
- Adjust the `throwForce` value in the `ThrowObject` script to control the throwing distance.
- Customize animations and UI elements to fit your game's theme.
- Ensure the player's stats (health, hunger, thirst) are balanced with the game mechanics.
*/