-
Notifications
You must be signed in to change notification settings - Fork 1
/
PurplePanel.cs
39 lines (32 loc) · 929 Bytes
/
PurplePanel.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PurplePanel : MonoBehaviour
{
private float minInterval = 2.0f;
private float maxInterval = 5.0f;
private float interval, minX, maxX;
// Reference to OrangePanel Rigidbody
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
public void Initialize(float left, float right)
{
minX = left;
maxX = right;
interval = Random.Range(minInterval, maxInterval);
// Cancel previous invokes
CancelInvoke();
// Reposition panel every interval
InvokeRepeating("updatePosition", interval, interval);
}
// Updates panel's position every interval (seconds)
private void updatePosition()
{
Vector3 pos = rb.position;
pos.x = Random.Range(minX, maxX);
rb.position = pos;
}
}