-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerscript_2dplatformer.cs
More file actions
144 lines (124 loc) · 4.17 KB
/
Playerscript_2dplatformer.cs
File metadata and controls
144 lines (124 loc) · 4.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//------------------------
// My Ideas
// Endless runner
// Avoid obstacles(Trees, you bounce back a bit if you hit it)
// Pickup objects
// Powerups(faster, higher highscore)
// powerdowns( slower, less highscore, restart, reverse controlls)
// or use the trees as real obstacles?
// High score
//
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Playerscript_2dplatformer: MonoBehaviour {
public GUIText countext;
public GUIText wintext;
private int count;
public float speed = 0.3f;
public float jump = 1.0f;
public Transform startposition;
public int wincount = 4;
// Use this for initialization
void Start() {
print("Hej");
count = 0;
SetCountText();
wintext.text = "";
}
// Update is called once per frame
void Update() {
}
void FixedUpdate() {
PlayerMovement();
QuitApp();
}
public void PlayerMovement() {
if (Input.anyKey) {
if (Input.GetKeyDown(KeyCode.W)) {
// print ("W is pressed");
this.transform.Translate(new Vector2(0, jump));
}
if (Input.GetKey(KeyCode.A)) {
// print ("A is pressed");
this.transform.Translate(new Vector2(-speed, 0));
}
if (Input.GetKey(KeyCode.S)) {
// print ("S is pressed");
this.transform.Translate(new Vector2(0, -speed));
}
if (Input.GetKey(KeyCode.D)) {
// print ("D is pressed");
this.transform.Translate(new Vector2(speed, 0));
}
if (Input.GetKey(KeyCode.R)) {
// reset to startposition aka origo.
this.transform.position = startposition.position;
this.transform.rotation = startposition.rotation;
// print ("R is pressed");
}
} else {
//print ("nothing is pressed");
}
// print ("Goodbye");
}
void QuitApp() {
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.Quit();
}
}
// On Collision something something
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "wall") {
print("Collision has been dectected");
}
if (collision.gameObject.tag == "Checkpoint") {
print("Checkpoint has been dectected");
}
if (collision.gameObject.tag == "start") {
print("start has been dectected");
}
if (collision.gameObject.tag == "end") {
print("end has been dectected");
}
if (collision.gameObject.tag == "Obstacle") {
StartCoroutine(HitObstacle());
print("Obstacle hit");
}
}
void OnTriggerStay(Collider trigger) {
if (trigger.gameObject.tag == "Ladder") {
print("Ladder is enabled.");
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Pickup") {
print("Picked up object");
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
if (other.gameObject.tag == "Speedboost") {
StartCoroutine(StopSpeedBoost());
print("SpeedBoost");
}
}
void SetCountText() {
countext.text = "Count" + count.ToString();
if (count >= wincount) {
wintext.text = "You win!";
Application.LoadLevel("level02");
}
}
IEnumerator StopSpeedBoost() {
speed = 1.0F;
yield return new WaitForSeconds(3F);
speed = 0.3;
print("speedboost over");
}
IEnumerator HitObstacle() {
speed = 0.2F;
yield return new WaitForSeconds(3F);
speed = 0.3F;
}
}