-
Notifications
You must be signed in to change notification settings - Fork 0
/
Platform.cs
75 lines (64 loc) · 1.72 KB
/
Platform.cs
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
using UnityEngine;
using System.Collections;
public class Platform : MonoBehaviour {
public float xSpeed = -0.4f;
public float ySpeed = 0;
public int pointValue = 0;
private Vector2 newSpeed;
private Rigidbody2D rb2D;
private Renderer rndr;
private SceneManager sceneMgr;
void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
rndr = GetComponent<Renderer>();
sceneMgr = GameObject.Find ("SceneManager").GetComponent<SceneManager>();
}
// Use this for initialization
void Start ()
{
newSpeed = new Vector2(sceneMgr.platformSpeed, ySpeed);
//rb2D.AddForce(newSpeed);
if (!rndr.isVisible)
sceneMgr.numPlatformsOffScreen++;
}
void FixedUpdate()
{
bool stoppedMoving = false;
if (sceneMgr.gameState == "playing")
{
stoppedMoving = false;
Vector3 move = new Vector3(sceneMgr.platformSpeed, 0, 0);
transform.position += move * Time.deltaTime;
}
else if (!stoppedMoving && sceneMgr.gameState != "playing")
{
stoppedMoving = true;
rb2D.AddForce(-newSpeed);
rb2D.isKinematic = false;
rb2D.gravityScale = Random.Range(-0.2f, -0.5f);
if (sceneMgr.howLevelEnded == "felltodeath")
rb2D.gravityScale *= -1;
gameObject.GetComponent<EdgeCollider2D>().enabled = false;
}
// else if (!stoppedMoving && sceneMgr.gameState == "felltodeath")
// {
// stoppedMoving = true;
// rb2D.AddForce(-newSpeed);
// rb2D.isKinematic = false;
// rb2D.gravityScale = Random.Range(-0.2f, -0.5f);
// }
}
// after we scroll off the screen
void OnBecameInvisible ()
{
//if (sceneMgr.platforms.Count > 0)
// sceneMgr.platforms.RemoveAt(0);
Destroy(gameObject);
sceneMgr.numPlatforms--; //decrement this when player jumps
}
void OnBecameVisible()
{
sceneMgr.numPlatformsOffScreen--;
}
}