Repository navigation
Expand file tree
/
Copy pathTargetX.cs
More file actions
88 lines (67 loc) · 2.53 KB
/
Copy pathTargetX.cs
File metadata and controls
88 lines (67 loc) · 2.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetX : MonoBehaviour
{
private Rigidbody rb;
private GameManagerX gameManagerX;
public int pointValue;
public GameObject explosionFx;
public float timeOnScreen = 1.0f;
private float minValueX = -3.75f; // the x value of the center of the left-most square
private float minValueY = -3.75f; // the y value of the center of the bottom-most square
private float spaceBetweenSquares = 2.5f; // the distance between the centers of squares on the game board
void Start()
{
rb = GetComponent<Rigidbody>();
gameManagerX = GameObject.Find("Game Manager").GetComponent<GameManagerX>();
transform.position = RandomSpawnPosition();
StartCoroutine(RemoveObjectRoutine()); // begin timer before target leaves screen
}
// When target is clicked, destroy it, update score, and generate explosion
private void OnMouseDown()
{
if (gameManagerX.isGameActive)
{
Destroy(gameObject);
gameManagerX.UpdateScore(pointValue);
Explode();
}
}
// Generate a random spawn position based on a random index from 0 to 3
Vector3 RandomSpawnPosition()
{
float spawnPosX = minValueX + (RandomSquareIndex() * spaceBetweenSquares);
float spawnPosY = minValueY + (RandomSquareIndex() * spaceBetweenSquares);
Vector3 spawnPosition = new Vector3(spawnPosX, spawnPosY, 0);
return spawnPosition;
}
// Generates random square index from 0 to 3, which determines which square the target will appear in
int RandomSquareIndex ()
{
return Random.Range(0, 4);
}
// If target that is NOT the bad object collides with sensor, trigger game over
private void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
if (other.gameObject.CompareTag("Sensor") && !gameObject.CompareTag("Bad"))
{
gameManagerX.GameOver();
}
}
// Display explosion particle at object's position
void Explode ()
{
Instantiate(explosionFx, transform.position, explosionFx.transform.rotation);
}
// After a delay, Moves the object behind background so it collides with the Sensor object
IEnumerator RemoveObjectRoutine ()
{
yield return new WaitForSeconds(timeOnScreen);
if (gameManagerX.isGameActive)
{
transform.Translate(Vector3.forward * 5, Space.World);
}
}
}