added gun models and enemy spawner
This commit is contained in:
@@ -3,8 +3,8 @@ using UnityEngine;
|
||||
public class FirstPersonController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
public float walkSpeed = 8f;
|
||||
public float runSpeed = 14f;
|
||||
public float walkSpeed = 50f; // Boosted from 8f to overcome collision issues
|
||||
public float runSpeed = 80f; // Boosted from 14f
|
||||
public float jumpHeight = 2.5f;
|
||||
public float gravity = -20f;
|
||||
|
||||
@@ -23,66 +23,96 @@ public class FirstPersonController : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
Debug.Log( "Starting game" );
|
||||
// Get the CharacterController component
|
||||
controller = GetComponent<CharacterController>();
|
||||
Debug.Log("Starting game");
|
||||
|
||||
// If no camera is assigned, try to find one
|
||||
if (playerCamera == null)
|
||||
// FORCE NORMAL TIME (in case something external changed it)
|
||||
Time.timeScale = 1f;
|
||||
|
||||
controller = GetComponent<CharacterController>();
|
||||
|
||||
if (controller == null)
|
||||
{
|
||||
playerCamera = GetComponentInChildren<Camera>();
|
||||
Debug.LogError("FirstPersonController: No CharacterController found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Lock and hide the cursor
|
||||
if (playerCamera == null)
|
||||
playerCamera = GetComponentInChildren<Camera>();
|
||||
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
// Check if player is on the ground
|
||||
if (controller == null) return;
|
||||
|
||||
isGrounded = controller.isGrounded;
|
||||
|
||||
// Reset vertical velocity when grounded
|
||||
if (isGrounded && velocity.y < 0)
|
||||
{
|
||||
velocity.y = -2f;
|
||||
|
||||
// ─── Movement via direct KeyCode ───
|
||||
float moveX = 0f;
|
||||
float moveZ = 0f;
|
||||
|
||||
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) moveZ += 1f;
|
||||
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) moveZ -= 1f;
|
||||
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) moveX -= 1f;
|
||||
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) moveX += 1f;
|
||||
|
||||
// DEBUG: Log once when W is first pressed
|
||||
if (Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
Debug.Log($"[FPC DEBUG] W pressed | controller.enabled={controller.enabled} | position={transform.position} | isGrounded={isGrounded}");
|
||||
Debug.Log($"[FPC DEBUG] Time.timeScale={Time.timeScale} | Time.deltaTime={Time.deltaTime} | walkSpeed={walkSpeed}");
|
||||
}
|
||||
|
||||
// Get movement input
|
||||
float moveX = Input.GetAxis("Horizontal"); // A/D or Left/Right arrows
|
||||
float moveZ = Input.GetAxis("Vertical"); // W/S or Up/Down arrows
|
||||
|
||||
// Calculate movement direction
|
||||
Vector3 move = transform.right * moveX + transform.forward * moveZ;
|
||||
|
||||
// Determine current speed (run if Shift is held)
|
||||
if (move.magnitude > 1f)
|
||||
move.Normalize();
|
||||
|
||||
float currentSpeed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
|
||||
|
||||
// Move the character
|
||||
|
||||
Vector3 posBefore = transform.position;
|
||||
controller.Move(move * currentSpeed * Time.deltaTime);
|
||||
|
||||
// Jumping
|
||||
if (Input.GetButtonDown("Jump") && isGrounded)
|
||||
|
||||
// DEBUG: Log if we tried to move but didn't
|
||||
if (move.magnitude > 0f && Input.GetKeyDown(KeyCode.W))
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
Vector3 posAfter = transform.position;
|
||||
Debug.Log($"[FPC DEBUG] Move attempt: delta={move * currentSpeed * Time.deltaTime} | actualDelta={(posAfter - posBefore)} | controller.height={controller.height} | controller.radius={controller.radius}");
|
||||
|
||||
// Check what we're colliding with
|
||||
Collider[] nearbyColliders = Physics.OverlapSphere(transform.position, controller.radius + 0.5f);
|
||||
Debug.Log($"[FPC DEBUG] Found {nearbyColliders.Length} colliders near player");
|
||||
foreach (Collider col in nearbyColliders)
|
||||
{
|
||||
if (col != controller && !(col is CharacterController))
|
||||
Debug.LogWarning($"[FPC DEBUG] Nearby collider: {col.gameObject.name} on layer {LayerMask.LayerToName(col.gameObject.layer)}");
|
||||
}
|
||||
}
|
||||
|
||||
// Apply gravity
|
||||
// Jumping
|
||||
if (Input.GetKey(KeyCode.Space) && isGrounded)
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
|
||||
// Gravity
|
||||
velocity.y += gravity * Time.deltaTime;
|
||||
controller.Move(velocity * Time.deltaTime);
|
||||
|
||||
// Mouse look
|
||||
HandleMouseLook();
|
||||
|
||||
// Press Escape to unlock cursor
|
||||
// Escape to unlock cursor
|
||||
if (Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.None;
|
||||
Cursor.visible = true;
|
||||
}
|
||||
|
||||
// Click to lock cursor again
|
||||
// Click to re-lock cursor
|
||||
if (Input.GetMouseButtonDown(0) && Cursor.lockState == CursorLockMode.None)
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
@@ -92,16 +122,13 @@ public class FirstPersonController : MonoBehaviour
|
||||
|
||||
void HandleMouseLook()
|
||||
{
|
||||
// Get mouse input
|
||||
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
|
||||
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
|
||||
|
||||
// Rotate camera up/down (pitch)
|
||||
xRotation -= mouseY;
|
||||
xRotation = Mathf.Clamp(xRotation, -maxLookAngle, maxLookAngle);
|
||||
playerCamera.transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
||||
|
||||
// Rotate player left/right (yaw)
|
||||
transform.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user