-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
156 lines (135 loc) · 4.5 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Controller2D))]
public class Player : MonoBehaviour
{
public float maxJumpHeight = 4;
public float minJumpHeight = 1;
public float timetoJumpApex = .4f;
public float wallSlideSpeedMax = 3;
public Vector2 wallJumpClimb;
public Vector2 wallJumpOff;
public Vector2 wallLeap;
public float wallStickTime = .25f;
public float timeToWallUnstick;
float gravity;
Vector3 velocity;
float moveSpeed = 6;
float velocityXSmoothing;
float accelerationTimeAirborne = .2f;
float accelerationTimeGrounded = .1f;
float maxJumpVelocity;
float minJumpVelocity;
Vector2 directionalInput;
bool wallSliding;
int wallDirX;
internal Controller2D controller;
void Start()
{
controller = GetComponent<Controller2D>();
gravity = -(2 * maxJumpHeight) / Mathf.Pow(timetoJumpApex, 2);
maxJumpVelocity = Mathf.Abs(gravity) * timetoJumpApex;
minJumpVelocity = Mathf.Sqrt(2 * Mathf.Abs(gravity) * minJumpHeight);
print("Gravity:" + gravity + " Jump Velocity:" + maxJumpVelocity);
}
void Update()
{
CalculateVelocity();
HandleWallSliding();
controller.Move(velocity * Time.deltaTime, directionalInput);
if (controller.collisions.above || controller.collisions.below)
{
if (controller.collisions.slidingDownMaxSlope)
velocity.y += controller.collisions.slopeNormal.y * -gravity * Time.deltaTime;
else
velocity.y = 0;
}
}
public void SetDirectionalInput(Vector2 input)
{
directionalInput = input;
}
public void OnJumpInputDown()
{
if (wallSliding)
{
if (wallDirX == directionalInput.x)
{
velocity.x = -wallDirX * wallJumpClimb.x;
velocity.y = wallJumpClimb.y;
}
else if (directionalInput.x == 0)
{
velocity.x = -wallDirX * wallJumpOff.x;
velocity.y = wallJumpOff.y;
}
else
{
velocity.x = -wallDirX * wallLeap.x;
velocity.y = wallLeap.y;
}
}
if (controller.collisions.below)
{
if(controller.collisions.slidingDownMaxSlope)
{
if(directionalInput.x != -Mathf.Sign(controller.collisions.slopeNormal.x))
{
velocity.y = maxJumpVelocity * controller.collisions.slopeNormal.y;
velocity.x = maxJumpVelocity * controller.collisions.slopeNormal.x;
}
}
velocity.y = maxJumpVelocity;
}
}
public void OnJumpInputUp()
{
if (velocity.y > minJumpVelocity)
{
velocity.y = minJumpVelocity;
}
}
public void slingShot(Vector2 slingshot){
Vector2 temp = transform.position;
temp = slingshot - temp;
temp.Normalize();
velocity = 50 * temp;
}
void CalculateVelocity()
{
float targetVelocityX = directionalInput.x * moveSpeed;
velocity.x = Mathf.SmoothDamp(velocity.x, targetVelocityX, ref velocityXSmoothing, (controller.collisions.below) ? accelerationTimeGrounded : accelerationTimeAirborne);
velocity.y += gravity * Time.deltaTime;
}
void HandleWallSliding()
{
wallDirX = (controller.collisions.left) ? -1 : 1;
wallSliding = false;
if ((controller.collisions.left || controller.collisions.right) && !controller.collisions.below && velocity.y < 0)
{
wallSliding = true;
if (velocity.y < -wallSlideSpeedMax)
{
velocity.y = -wallSlideSpeedMax;
}
if (timeToWallUnstick > 0)
{
velocity.x = 0;
velocityXSmoothing = 0;
if (directionalInput.x != wallDirX && directionalInput.x != 0)
{
timeToWallUnstick -= Time.deltaTime;
}
else
{
timeToWallUnstick = wallStickTime;
}
}
else
{
timeToWallUnstick = wallStickTime;
}
}
}
}