-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlatformController.cs
51 lines (40 loc) · 1.14 KB
/
PlatformController.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformController : MonoBehaviour {
//Declaring all float
public float velocity =23f;
public float platformMin = -20;
public float platformMax = 20;
public GameObject Player1;
//Declaring all boolean
bool moveLeft = true;
bool moveRight= true;
Rigidbody P1;
//Initialization
void Start () {
P1 = Player1.GetComponent<Rigidbody> ();
}
void Update () {
//Platform is controlled using Right and Left Arrow keys
if (Input.GetKey (KeyCode.LeftArrow) && moveLeft == true) {
P1.velocity = new Vector3 (-velocity, 0f, 0f);
} else if (Input.GetKey (KeyCode.RightArrow) && moveRight == true) {
P1.velocity = new Vector3 (velocity, 0f, 0f);
} else {
P1.velocity = new Vector3 (0f, 0f, 0f);
}
//Prevents player from colliding with WorldBounds:East/Right
if(Player1.transform.position.x >= platformMax){
moveRight = false;
}else{
moveRight = true;
}
//Prevents player from colliding with WorldBounds:West/Left
if(Player1.transform.position.x <= platformMin){
moveLeft = false;
}else{
moveLeft = true;
}
}
}