forked from katiSteven/Very-Simple-2D-Movement-in-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputGuy.cs
More file actions
92 lines (78 loc) · 2.46 KB
/
OutputGuy.cs
File metadata and controls
92 lines (78 loc) · 2.46 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
using UnityEngine;
using System.Collections;
using System.Text;
public class OutputGuy : MonoBehaviour {
public float Speed = 0;
public float Jump_Force_yAxis = 0;
private bool isJumping = false;
private bool foundFooting = false;
private bool pulledDown = true;
private Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
// adds force to jump upwards
void GoUp(){
print("going up");
rb.AddForce(new Vector2(0,Jump_Force_yAxis+12.5f), ForceMode2D.Impulse);
isJumping = false;
pulledDown = true;
}
// Constant gravity to pull down the player when NOT in contact with the collider of the 'platform GameObject'
void goDown(){
print("going down");
rb.AddForce(new Vector2(0,-1), ForceMode2D.Impulse);
isJumping = false;
pulledDown = true;
}
//checking if the object is in contact with the 'platform GameObject'
void OnCollisionStay2D(Collision2D col){
GameObject obj = col.gameObject;
if (obj.GetComponent<BoxCollider2D> ().transform.position.y <= this.gameObject.transform.position.y) {
if (obj.GetComponent<Platform> ()) {
foundFooting = true;
pulledDown = false;
return;
} else {
foundFooting = false;
pulledDown = true;
}
} else {
return;
}
}
void OnCollisionExit2D(){
foundFooting = false;
pulledDown = true;
}
// Update is called once per frame
void Update (){
// Enabling Jump bool when Space is pressed
if (Input.GetKeyDown (KeyCode.Space)) {
isJumping = true;
} else {
isJumping = false;
}
//Enabling OR disabling jump bool conditions
if(isJumping && foundFooting && !pulledDown){
GoUp();
}
//Enabling OR disabling gravity bool conditions
if(!isJumping && !foundFooting && pulledDown){
goDown();
}
//Right movement thorough Right arrow key
if (Input.GetKey (KeyCode.RightArrow)) {
Vector3 vehicleposition = new Vector3 (transform.position.x * Time.deltaTime, transform.position.y, transform.position.z);
vehicleposition.x = transform.position.x + Speed / 100;
transform.position = vehicleposition;
}
//Right movement thorough Left arrow key
if (Input.GetKey (KeyCode.LeftArrow)) {
Vector3 vehicleposition = new Vector3 (transform.position.x * Time.deltaTime, transform.position.y, transform.position.z);
vehicleposition.x = transform.position.x - Speed / 100;
transform.position = vehicleposition;
}
}
}