-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
216 lines (167 loc) · 5.96 KB
/
Player.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float gravity;
public Vector2 velocity;
public float maxXVelocity = 100;
public float maxAcceleration = 10;
public float acceleration = 10;
public float distance = 0;
public float jumpVelocity = 50;
public float groundHeight = 0;
public bool isGrounded = false;
public bool isHoldingJump = false;
public float maxHoldJumpTime = 0.3f;
public float maxMaxHoldJumpTime = 0.4f;
public float holdJumpTimer = 0.0f;
public float jumpGroundThreshold = 2;
public bool isDead = false;
public LayerMask groundLayerMask;
public LayerMask obstacleLayerMask;
GroundFall fall;
// Start is called before the first frame update
void Start()
{
}
void Update()
{
Vector2 pos = transform.position;
float groundDistance = Mathf.Abs(pos.y - groundHeight);
if (isGrounded || groundDistance <= jumpGroundThreshold)
{
if (Input.GetKeyDown(KeyCode.Space))
{
isGrounded = false;
velocity.y = jumpVelocity;
isHoldingJump = true;
holdJumpTimer = 0;
if (fall != null)
{
fall.player = null;
fall = null;
}
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isHoldingJump = false;
}
}
private void FixedUpdate()
{
Vector2 pos = transform.position;
if (isDead)
{
return;
}
if (pos.y < -20)
{
isDead = true;
}
if (!isGrounded)
{
if (isHoldingJump)
{
holdJumpTimer += Time.fixedDeltaTime;
if (holdJumpTimer >= maxHoldJumpTime)
{
isHoldingJump = false;
}
}
pos.y += velocity.y * Time.fixedDeltaTime;
if (!isHoldingJump)
{
velocity.y += gravity * Time.fixedDeltaTime;
}
Vector2 rayOrigin = new Vector2(pos.x + 0.7f, pos.y);
Vector2 rayDirection = Vector2.up;
float rayDistance = velocity.y * Time.fixedDeltaTime;
RaycastHit2D hit2D = Physics2D.Raycast(rayOrigin, rayDirection, rayDistance);
if (hit2D.collider != null)
{
Ground ground = hit2D.collider.GetComponent<Ground>();
if (ground != null)
{
if (pos.y >= ground.groundHeight)
{
groundHeight = ground.groundHeight;
pos.y = groundHeight;
velocity.y = 0;
isGrounded = true;
}
fall = ground.GetComponent<GroundFall>();
if (fall != null)
{
fall.player = this;
}
}
}
Debug.DrawRay(rayOrigin, rayDirection * rayDistance, Color.red);
Vector2 wallOrigin = new Vector2(pos.x, pos.y);
RaycastHit2D wallHit = Physics2D.Raycast(wallOrigin, Vector2.right, velocity.x * Time.fixedDeltaTime, groundLayerMask);
if (wallHit.collider != null)
{
Ground ground = wallHit.collider.GetComponent<Ground>();
if (ground != null)
{
if (pos.y < ground.groundHeight)
{
velocity.x = 0;
}
}
}
}
distance += velocity.x * Time.fixedDeltaTime;
if (isGrounded)
{
float velocityRatio = velocity.x / maxXVelocity;
acceleration = maxAcceleration * (1 - velocityRatio);
maxHoldJumpTime = maxMaxHoldJumpTime * velocityRatio;
velocity.x += acceleration * Time.fixedDeltaTime;
if (velocity.x >= maxXVelocity)
{
velocity.x = maxXVelocity;
}
Vector2 rayOrigin = new Vector2(pos.x - 0.7f, pos.y);
Vector2 rayDirection = Vector2.up;
float rayDistance = velocity.y * Time.fixedDeltaTime;
if (fall != null)
{
rayDistance = -fall.fallSpeed * Time.fixedDeltaTime;
}
RaycastHit2D hit2D = Physics2D.Raycast(rayOrigin, rayDirection, rayDistance);
if (hit2D.collider == null)
{
isGrounded = false;
}
Debug.DrawRay(rayOrigin, rayDirection * rayDistance, Color.yellow);
}
Vector2 obstOrigin = new Vector2(pos.x, pos.y);
RaycastHit2D obstHitX = Physics2D.Raycast(obstOrigin, Vector2.right, velocity.x * Time.fixedDeltaTime, obstacleLayerMask);
if (obstHitX.collider != null)
{
Obstacle obstacle = obstHitX.collider.GetComponent<Obstacle>();
if (obstacle != null)
{
hitObstacle(obstacle);
}
}
RaycastHit2D obstHitY = Physics2D.Raycast(obstOrigin, Vector2.up, velocity.y * Time.fixedDeltaTime, obstacleLayerMask);
if (obstHitY.collider != null)
{
Obstacle obstacle = obstHitY.collider.GetComponent<Obstacle>();
if (obstacle != null)
{
hitObstacle(obstacle);
}
}
transform.position = pos;
}
void hitObstacle(Obstacle obstacle)
{
Destroy(obstacle.gameObject);
velocity.x *= 0.7f;
}
}