Created the best movement controller there is

This commit is contained in:
2024-12-02 22:38:05 -08:00
parent 67b5eaa549
commit 1563222b01
14 changed files with 1062 additions and 26 deletions

View File

@@ -0,0 +1,71 @@
using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
public class playerController : MonoBehaviour
{
public float moveSpeed;
public float jumpStrength;
public float airSpeed;
public float mouseSens;
InputAction move;
InputAction jump;
InputAction look;
float camVertRot = 0f;
bool grounded;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
move = InputSystem.actions.FindAction("Move");
jump = InputSystem.actions.FindAction("Jump");
look = InputSystem.actions.FindAction("Look");
}
void Update()
{
Vector2 movement = move.ReadValue<Vector2>().normalized;
Vector2 lookment = look.ReadValue<Vector2>();
if (grounded) {
GetComponent<Rigidbody>().linearVelocity = transform.TransformVector(new Vector3 (
movement.x * moveSpeed,
(jump.IsPressed()) ? jumpStrength : 0,
movement.y * moveSpeed
));
} else {
GetComponent<Rigidbody>().AddForce(
movement.x * airSpeed,
0,
movement.y * airSpeed
);
}
gameObject.transform.Rotate(0, lookment.x * mouseSens, 0);
camVertRot -= lookment.y * mouseSens;
camVertRot = Mathf.Clamp(camVertRot, -90f, 90f);
foreach (Transform child in transform) {
child.localEulerAngles = Vector3.right * camVertRot;
}
}
private bool checkgrounded(Collision col) {
List<ContactPoint> points = new List<ContactPoint>();
col.GetContacts(points);
foreach (ContactPoint contactPoint in points) {
if (contactPoint.point.y < gameObject.transform.position.y - 0.95) {
return true;
}
}
return false;
}
private void OnCollisionStay(Collision col) {
if (checkgrounded(col))
grounded = true;
}
private void OnCollisionExit(Collision col) {
if (!checkgrounded(col))
grounded = false;
}
}