-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRock.cs
More file actions
42 lines (34 loc) · 1.01 KB
/
Rock.cs
File metadata and controls
42 lines (34 loc) · 1.01 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rock : MonoBehaviour {
int rockCounter;
const float MinImpulseForce = 1f;
const float MaxImpulseForce = 2f;
Timer lifeTime;
const float lifeTimeInSeconds = 3f;
void Start()
{
// apply impulse force to get game object moving
float angle = Random.Range(0, 2 * Mathf.PI);
Vector2 direction = new Vector2(
Mathf.Cos(angle), Mathf.Sin(angle));
float magnitude = Random.Range(MinImpulseForce, MaxImpulseForce);
GetComponent<Rigidbody2D>().AddForce(
direction * magnitude,
ForceMode2D.Impulse);
lifeTime = gameObject.AddComponent<Timer>();
lifeTime.Duration = lifeTimeInSeconds;
lifeTime.Run();
}
private void Update()
{
if (lifeTime.Finished)
Destroy(this.gameObject);
}
private void OnBecameInvisible()
{
enabled = false;
Destroy(this.gameObject);
}
}