-
Notifications
You must be signed in to change notification settings - Fork 0
/
Character_Motor.cs
176 lines (144 loc) · 5.32 KB
/
Character_Motor.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// Class description
// Convert the 33D Vector to world Space
// Update the Character Rotation with Camera Rotation
public class Character_Motor : MonoBehaviour {
public static Character_Motor Instance;
public Vector3 MoveVector;
public Vector3 SlideVector;
public float TerminalVelocity = 50f;
public float JumpVelocity = 80f;
public float Gravity = 9.8f;
public float SlideLimit = 0.9f;
public float MaxMagnitudeSlide = 0.4f;
private bool Jumping = false;
public bool InvertedModel = true;
Dictionary<Animation_Manager.MotionStateList, float> speedLimits;
public bool IsJumping {
get { return Jumping; }
set { Jumping = value; }
}
void Awake()
{
//Store an Instance of itself
Instance = this;
speedLimits = new Dictionary<Animation_Manager.MotionStateList, float>();
//Create variables for your different speeds
speedLimits.Add(Animation_Manager.MotionStateList.Backward, 5f);
speedLimits.Add(Animation_Manager.MotionStateList.Forward, 10f);
speedLimits.Add(Animation_Manager.MotionStateList.Left, 8f);
speedLimits.Add(Animation_Manager.MotionStateList.Right, 8f);
speedLimits.Add(Animation_Manager.MotionStateList.RightForward, 9f);
speedLimits.Add(Animation_Manager.MotionStateList.LeftForward, 9f);
speedLimits.Add(Animation_Manager.MotionStateList.RightBackward, 4f);
speedLimits.Add(Animation_Manager.MotionStateList.LeftBackward, 4f);
speedLimits.Add(Animation_Manager.MotionStateList.Stationary, 0f);
}
public void ControlledUpdate()
{
AlignCharacterToCameraDirection();
ProcessMotion();
Animation_Manager.Instance.CurrentMotionState();
}
public void ProcessMotion()
{
// Save MoveVector.z and reapply as VerticalVelocity
ApplyGravity();
//Convert Vector to World Space
Vector3 WorldPosition;
// If model is inverted then invert Z and Y get world position and revert Z and Y
if (InvertedModel) {
MoveVector = MoveVector.InvertYZ ();
WorldPosition = transform.TransformDirection (MoveVector);
MoveVector = MoveVector.RevertYZ ();
} else {
WorldPosition = transform.TransformDirection (MoveVector);
}
float savedY = WorldPosition.y;
WorldPosition.y = 0;
float magnitude = WorldPosition.sqrMagnitude;
//Normalize Vector
if (magnitude > 1)
WorldPosition.Normalize();
WorldPosition = Slide (WorldPosition);
//Multiply magnifier
WorldPosition = WorldPosition * SpeedLimit();
WorldPosition.y = savedY;
//Convert to unit/second
WorldPosition *= Time.deltaTime;
//Move character
Character_Manager.Instance.CharacterControllerComponent.Move(WorldPosition);
}
public void AlignCharacterToCameraDirection()
{
if (MoveVector.x != 0 || MoveVector.z != 0) {
//Replace the characters Y rotation with the cameras Y rotation
Transform cameraRotation = Camera_Manager.Instance.transform;
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
cameraRotation.eulerAngles.y,
transform.eulerAngles.z);
}
}
void ApplyGravity() {
//Check if our Y vector is less than TerminalVelcocity
//If yes, apply gravity
//Check if the character is grounded, if it is then apply a small amount of gravity
if (Character_Manager.Instance.CharacterControllerComponent.isGrounded) {
if (IsJumping)
IsJumping = false;
else
MoveVector.y = 0.01f;
}
else if (MoveVector.y > -TerminalVelocity)
{
MoveVector.y -= Gravity;
if (MoveVector.y < -TerminalVelocity)
MoveVector.y = -TerminalVelocity;
}
}
public void Jump() {
//Check if the character is grounded
//If it is, move our character (VerticalVelocity)
if (Character_Manager.Instance.CharacterControllerComponent.isGrounded)
{
MoveVector.y = JumpVelocity;
IsJumping = true;
}
}
public Vector3 Slide(Vector3 WorldPosition){
//First check if the character is on the ground if not return;
if (!Character_Manager.Instance.CharacterControllerComponent.isGrounded)
return WorldPosition;
//Zero out the slideVector
SlideVector = Vector3.zero;
RaycastHit hitInfo;
//Move our raycast position up one unit in Y and cast it down
if (Physics.Raycast(transform.position + Vector3.up, Vector3.down, out hitInfo)) {
//Check if the information from the normal.y is less than the our slide limit
if (hitInfo.normal.y < SlideLimit) {
//If it is, add our slideVector to our moveVector
//Get the raycastInfo for each normal and store it as the slideVector
SlideVector = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
}
//If the magnitude of our slideVector (slide speed) is too great the we should lose the controls of our character
if (SlideVector.magnitude < MaxMagnitudeSlide) {
WorldPosition += SlideVector;
} else {
//If it is, set our moveVector to our slideVector
WorldPosition = SlideVector;
}
}
return WorldPosition;
}
float SpeedLimit() {
//Our switch will be our CharacterMotionState assigned within Animation_Manager
//Our case will be MotionStateList
//Set your speed accordingly
//Assign your sliding speed outside of your switch/case because it falls outside the MotionStateList
if (SlideVector.magnitude > MaxMagnitudeSlide)
return 15f;
return speedLimits[Animation_Manager.Instance.CharacterMotionState];
}
}