-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerControllerFP.cs
More file actions
142 lines (101 loc) · 4.72 KB
/
PlayerControllerFP.cs
File metadata and controls
142 lines (101 loc) · 4.72 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// basic controller to work with animations. Still need to add jump
public class PlayerControllerFP : MonoBehaviour
{
[SerializeField] Transform playercamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 6f;
[SerializeField] float gravity = -13f;
[SerializeField] bool lockCursor = true;
[SerializeField] [Range(0f, .5f)] float moveSmoothTime = .3f;
[SerializeField] [Range(0f, .5f)] float mouseSmoothTime = .03f;
public Animator animator;
public float move;
float cameraPitch = 0f;
float velocityY = 0f;
CharacterController controller = null;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
private void Start()
{
// mouse lock
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
private void Update()
{
UpdateMouseLook();
UpdateMovement();
Dash();
Sprint();
}
//basic look
void UpdateMouseLook ()
{
Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetMouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90f, 90f);
playercamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
//basic movement
void UpdateMovement()
{
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocityY = 0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * walkSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
// float test for animator
animator.SetFloat("Vertical", velocity.z);
animator.SetFloat("Horizontal", velocity.x);
}
// trigger test for animator. Quick movement in direction of movement
private void Dash()
{
if (Input.GetButtonDown("Fire2"))
{
animator.SetTrigger("Dash");
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocityY = 0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y * 5 + transform.right * currentDir.x * 5) * walkSpeed * 30 + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
}
}
// bool test for animatior. Make player run faster in a straight line. Error it also goes back
private void Sprint()
{
if (Input.GetButton("Fire2"))
{
animator.SetBool("Sprint", true);
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (controller.isGrounded)
velocityY = 0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y ) * walkSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
}
else
{
animator.SetBool("Sprint", false);
}
}
}