-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerMovement.cs
119 lines (95 loc) · 2.65 KB
/
PlayerMovement.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
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
using System.Collections;
using System.Collections.Generic;
//using System;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5;
public float jumpCount = 3;
int jumpHeight = 15;
//int lives =
//int y_thresh
//if under a certain y_thresh, subtrack life
bool isGrounded;
Rigidbody2D rb2d;
[SerializeField]
Transform groundCheck;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
double abs(double val)
{
if (val < 0)
{
return (-1) * val;
}
else
{
return val;
}
}
private void FixedUpdate()
{
if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
if(Input.GetKey(KeyCode.RightArrow) || Input.GetKey("d"))
{
if(abs(rb2d.velocity.x) < moveSpeed)
{
rb2d.velocity += new Vector2(moveSpeed, 0);
}
}
if(Input.GetKey(KeyCode.LeftArrow) || Input.GetKey("a"))
{
if(abs(rb2d.velocity.x) < moveSpeed )
{
rb2d.velocity += new Vector2(0 - moveSpeed, 0);
}
}
if (!Input.anyKey)
{
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
if ((Input.GetKey("space") || Input.GetKey(KeyCode.UpArrow)) && isGrounded)
{
//rb2d.velocity = new Vector2(rb2d.velocity.x, jumpHeight);
rb2d.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
}
}
}
//private void FixedUpdate()
//{
// if (Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground")))
// {
// isGrounded = true;
// }
// else
// {
// isGrounded = false;
// }
// if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey("d"))
// {
// rb2d.velocity = new Vector2(moveSpeed, 0);
// }
// else if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey("a"))
// {
// rb2d.velocity = new Vector2(0 - moveSpeed, 0);
// }
// else
// {
// rb2d.velocity = new Vector2(0, rb2d.velocity.y);
// }
// if ((Input.GetKey("space") || Input.GetKey(KeyCode.UpArrow)) && isGrounded)
// {
// rb2d.velocity = new Vector2(rb2d.velocity.x, jumpHeight);
// //gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 2), ForceMode2D.Impulse);
// }
//}