-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayercontroller.cs
134 lines (96 loc) · 3.49 KB
/
playercontroller.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//remember too put player on player tag
public class playercontroller : MonoBehaviour
{
[SerializeField] Transform PlayerCamera = null;
[SerializeField] float Mousesensetivity = 3.5f;
[SerializeField] float Walkspeed = 6.0f;
[SerializeField] float JumpHeight = 3f;
[SerializeField] float gravity = -13.0f;
[SerializeField][Range(0.0f, 0.5f)] float MoveSmoothTime = 0.3f;
[SerializeField][Range(0.0f, 0.5f)] float MouseSmoothTime = 0.03f;
void OnCollisionStay()
{
isGrounded = true;
}
public float speed = 10f;
public float jumpHeight = 5f;
[SerializeField] bool Lockedcursor = true;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
CharacterController controller = null;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirvelocity = Vector2.zero;
Vector2 currentmousedelta = Vector2.zero;
Vector2 currentmousedeltavelocity = Vector2.zero;
void Start()
{
controller = GetComponent<CharacterController>();
if (Lockedcursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
UpdateMouseLook();
updatemovement();
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if (Input.GetButton("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
void GroundCheck()
{
RaycastHit hit;
float distance = 1f;
Vector3 dir = new Vector3(0, -1);
if (Physics.Raycast(transform.position, dir, out hit, distance))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
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 * Mousesensetivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
PlayerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentmousedelta.x * Mousesensetivity);
}
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 = 0.0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * Walkspeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
}
}