Movement feels good :)

This commit is contained in:
2024-12-03 22:44:01 -08:00
parent 1563222b01
commit 9eed55d57b
11 changed files with 5484 additions and 25 deletions

View File

@@ -2,17 +2,21 @@ using UnityEngine;
using UnityEngine.InputSystem;
using System.Collections.Generic;
public class playerController : MonoBehaviour
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpStrength;
public float airSpeed;
public float mouseSens;
public float throwStrength;
public GameObject discharge;
public bool grounded;
InputAction move;
InputAction jump;
InputAction look;
InputAction click;
float camVertRot = 0f;
bool grounded;
Queue<GameObject> discharges;
void Start()
{
@@ -21,40 +25,98 @@ public class playerController : MonoBehaviour
move = InputSystem.actions.FindAction("Move");
jump = InputSystem.actions.FindAction("Jump");
look = InputSystem.actions.FindAction("Look");
discharges = new Queue<GameObject>();
}
void Update()
{
Vector2 movement = move.ReadValue<Vector2>().normalized;
Vector2 lookment = look.ReadValue<Vector2>();
if (grounded) {
GetComponent<Rigidbody>().linearVelocity = transform.TransformVector(new Vector3 (
if (grounded) MoveGrounded(movement);
else MoveAirborne(movement);
MoveCamera(lookment);
if (InputSystem.actions.FindAction("Attack").WasPressedThisFrame())
Throw();
if (InputSystem.actions.FindAction("Interact").WasPressedThisFrame())
discharges.Dequeue().GetComponent<Discharge>().Explode();
}
void MoveGrounded(Vector2 movement) {
Rigidbody rb = GetComponent<Rigidbody>();
if (jump.IsPressed()) {
Vector3 moveVelocity = transform.TransformVector (
movement.x * moveSpeed,
(jump.IsPressed()) ? jumpStrength : 0,
jumpStrength,
movement.y * moveSpeed
));
} else {
GetComponent<Rigidbody>().AddForce(
movement.x * airSpeed,
0,
movement.y * airSpeed
);
Vector3 currVelocity = new Vector3 (
rb.linearVelocity.x,
jumpStrength,
rb.linearVelocity.z
);
if (movement.Equals(Vector2.zero)) {
rb.linearVelocity =
transform.forward *
currVelocity.magnitude;
}
rb.linearVelocity =
moveVelocity.normalized * currVelocity.magnitude;
return;
}
rb.linearVelocity = transform.TransformVector(
movement.x * moveSpeed,
0,
movement.y * moveSpeed
);
}
void MoveAirborne(Vector2 movement) {
GetComponent<Rigidbody>().AddForce(transform.TransformVector(
movement.x * airSpeed,
0,
movement.y * airSpeed
));
}
void MoveCamera(Vector2 lookment) {
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;
}
}
camVertRot = Mathf.Clamp(camVertRot, -89f, 89f);
transform.GetChild(0).localEulerAngles =
Vector3.right * camVertRot;
}
void Throw() {
GameObject thrown = Instantiate(
discharge,
GetComponentInChildren<Transform>().position,
Quaternion.identity
);
Physics.IgnoreCollision(
GetComponent<Collider>(),
thrown.GetComponent<Collider>()
);
thrown.GetComponent<Rigidbody>().linearVelocity =
GetComponent<Rigidbody>().linearVelocity;
thrown.GetComponent<Rigidbody>().AddForce(
transform.GetChild(0).forward * throwStrength,
ForceMode.VelocityChange
);
discharges.Enqueue(thrown);
}
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;
}
if (contactPoint.point.y <
gameObject.transform.position.y - 0.95
) return true;
}
return false;
}