-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMoveModule.cs
More file actions
169 lines (131 loc) · 3.37 KB
/
MoveModule.cs
File metadata and controls
169 lines (131 loc) · 3.37 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveModule : MonoBehaviour {
public float Speed = 1f;
public float RotatingSpeed = 1f;
public float JumpForce = 1f;
public float AdditionalGravity = 3f;
[Space(10)]
[Header("MinStepMax")]
public float MaxSpeed = 15f;
public Vector3 LerpDragSpeed;
[Space(10)]
[Header("Dash parameters")]
public float DashForce = 1f;
Vector3 CurrentDirection = Vector3.zero;
Animator anim;
Rigidbody rb;
Camera cam;
Quaternion initial_rotation;
Quaternion other_rotation;
Quaternion target;
[HideInInspector] public bool is_jumping;
float height;
// Use this for initialization
void Start () {
Initialization();
}
// Update is called once per frame
void Update () {
AdjustPhysics();
AdjustRotation();
AdjustAnim();
}
public void PlayerMove(Vector2 direction)
{
CurrentDirection = direction;
if(direction.magnitude > 0.1f)
{
Move(cam.transform.right*direction.x);
Rotate(cam.transform.right*direction.x);
}
}
public void Dash()
{
anim.SetTrigger("Dash");
}
public void DashMove()
{
rb.velocity += transform.forward*DashForce;
}
void Move(Vector3 direction)
{
rb.AddForce(direction*Speed);
}
void Rotate(Vector3 direction)
{
target = (Vector3.Dot(direction, cam.transform.right) > 0) ? initial_rotation : other_rotation;
}
void AdjustRotation()
{
transform.rotation = Quaternion.Lerp(transform.rotation, target, RotatingSpeed*Time.deltaTime);
}
void AdjustAnim()
{
float speed_param = anim.GetFloat("Speed");
speed_param = Mathf.Clamp01(Mathf.Lerp(speed_param, rb.velocity.magnitude, LerpDragSpeed.y));
anim.SetFloat("Speed", speed_param);
}
void AdjustPhysics()
{
Ray ray = new Ray(transform.position, -Vector3.up);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, height*1.05f))
{
if(is_jumping)
{
anim.SetTrigger("Land");
}
is_jumping = false;
}
else
{
if(!is_jumping)
{
anim.SetTrigger("Fall");
}
is_jumping = true;
}
if(is_jumping)
{
rb.velocity += -Vector3.up*AdditionalGravity;
LerpDrag(LerpDragSpeed.x);
}
else
{
if(CurrentDirection.magnitude > 0.2f)
LerpDrag(LerpDragSpeed.x);
else
LerpDrag(LerpDragSpeed.z);
}
// CheckMaxSpeed();
}
void LerpDrag(float v)
{
rb.drag = Mathf.Lerp(rb.drag, v, LerpDragSpeed.z*Time.deltaTime);
}
void CheckMaxSpeed()
{
float current_limit = is_jumping ? MaxSpeed*5f : MaxSpeed;
// Debug.Log(current_limit);
if(rb.velocity.magnitude > MaxSpeed)
{
Debug.Log("Speed over " + current_limit.ToString());
}
Vector3 current_velocity = rb.velocity;
current_velocity = (current_velocity.magnitude > current_limit) ?
Vector3.Lerp(current_velocity, current_velocity.normalized*current_limit, LerpDragSpeed.y*Time.deltaTime) : current_velocity;
rb.velocity = current_velocity;
}
void Initialization()
{
cam = Camera.main;
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
initial_rotation = transform.rotation;
other_rotation = Quaternion.AngleAxis(180, Vector3.up);
target = initial_rotation;
height = GetComponent<Collider>().bounds.size.y/2f;
}
}