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; public float throwStrength; public GameObject discharge; public bool grounded; Vector3 ogPosition; Quaternion ogRotation; InputAction move; InputAction jump; InputAction look; InputAction click; float camVertRot = 0f; Queue discharges; float detonateCooldown = 0; void Start() { Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; move = InputSystem.actions.FindAction("Move"); jump = InputSystem.actions.FindAction("Jump"); look = InputSystem.actions.FindAction("Look"); discharges = new Queue(); ogPosition = transform.position; ogRotation = transform.rotation; } void Update() { Vector2 movement = move.ReadValue().normalized; Vector2 lookment = look.ReadValue(); if (grounded) MoveGrounded(movement); else MoveAirborne(movement); MoveCamera(lookment); if (InputSystem.actions.FindAction("Attack").WasPressedThisFrame()) Throw(); if (InputSystem.actions.FindAction("Interact").IsInProgress()) Detonate(); if (InputSystem.actions.FindAction("Reset").WasPressedThisFrame()) Reset(); } void MoveGrounded(Vector2 movement) { Rigidbody rb = GetComponent(); if (jump.IsPressed()) { Vector3 moveVelocity = transform.TransformVector ( movement.x * moveSpeed, jumpStrength, movement.y * moveSpeed ); 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().AddForce(transform.TransformVector( movement.x * airSpeed, 0, movement.y * airSpeed )); } void MoveCamera(Vector2 lookment) { gameObject.transform.Rotate(0, lookment.x * mouseSens, 0); camVertRot = Mathf.Clamp( camVertRot - (lookment.y * mouseSens), -89f, 89f ); transform.GetChild(0).localEulerAngles = Vector3.right * camVertRot; } void Throw() { GameObject thrown = Instantiate( discharge, GetComponentInChildren().position, Quaternion.identity ); Physics.IgnoreCollision( GetComponent(), thrown.GetComponent() ); thrown.GetComponent().linearVelocity = GetComponent().linearVelocity; thrown.GetComponent().AddForce( transform.GetChild(0).forward * throwStrength, ForceMode.VelocityChange ); discharges.Enqueue(thrown); detonateCooldown = Time.fixedTime; } void Detonate() { if (Time.fixedTime < detonateCooldown + 0.5F || discharges.Count == 0 ) return; detonateCooldown = Time.fixedTime; discharges.Dequeue().GetComponent().Explode(); } void Reset() { transform.position = ogPosition; transform.rotation = ogRotation; GetComponent().linearVelocity = Vector3.zero; } private bool checkgrounded(Collision col) { List points = new List(); 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; } }