简单的玩家预制体和脚本
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Agame
|
||||
{
|
||||
public class MouseLook : MonoBehaviour
|
||||
{
|
||||
[Header("Sensitivity")]
|
||||
[Tooltip("Degrees of rotation per pixel of mouse movement.")]
|
||||
public float mouseSensitivity = 0.12f;
|
||||
|
||||
[Header("Vertical Clamp")]
|
||||
public float minLookAngle = -80f;
|
||||
public float maxLookAngle = 80f;
|
||||
|
||||
[Header("References")]
|
||||
public Transform characterBody;
|
||||
|
||||
private float xRotation = 0f;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
LockCursor(true);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var kb = Keyboard.current;
|
||||
var mouse = Mouse.current;
|
||||
if (mouse == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Esc releases the cursor, left click grabs it again (editor-friendly)
|
||||
if (kb != null && kb.escapeKey.wasPressedThisFrame)
|
||||
{
|
||||
LockCursor(false);
|
||||
}
|
||||
if (mouse.leftButton.wasPressedThisFrame && Cursor.lockState != CursorLockMode.Locked)
|
||||
{
|
||||
LockCursor(true);
|
||||
}
|
||||
|
||||
if (Cursor.lockState != CursorLockMode.Locked)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Mouse delta is in pixels for the new Input System
|
||||
Vector2 delta = mouse.delta.ReadValue();
|
||||
float mouseX = delta.x * mouseSensitivity;
|
||||
float mouseY = delta.y * mouseSensitivity;
|
||||
|
||||
// Yaw the body left/right
|
||||
if (characterBody != null)
|
||||
{
|
||||
characterBody.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
|
||||
// Pitch the camera up/down
|
||||
xRotation -= mouseY;
|
||||
xRotation = Mathf.Clamp(xRotation, minLookAngle, maxLookAngle);
|
||||
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
|
||||
}
|
||||
|
||||
private void LockCursor(bool locked)
|
||||
{
|
||||
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
||||
Cursor.visible = !locked;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca68f61e4f5bf6b439395d4aa1b7334a
|
||||
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace Agame
|
||||
{
|
||||
[RequireComponent(typeof(CharacterController))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement")]
|
||||
public float moveSpeed = 5f;
|
||||
public float sprintSpeed = 8f;
|
||||
public float gravity = -20f;
|
||||
public float jumpHeight = 1.5f;
|
||||
|
||||
private CharacterController controller;
|
||||
private Vector3 velocity;
|
||||
private bool isGrounded;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
controller = GetComponent<CharacterController>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
var kb = Keyboard.current;
|
||||
if (kb == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
isGrounded = controller.isGrounded;
|
||||
if (isGrounded && velocity.y < 0f)
|
||||
{
|
||||
velocity.y = -2f;
|
||||
}
|
||||
|
||||
// WASD input
|
||||
float moveX = 0f;
|
||||
float moveZ = 0f;
|
||||
if (kb.aKey.isPressed) moveX -= 1f;
|
||||
if (kb.dKey.isPressed) moveX += 1f;
|
||||
if (kb.sKey.isPressed) moveZ -= 1f;
|
||||
if (kb.wKey.isPressed) moveZ += 1f;
|
||||
|
||||
// Move relative to the capsule's own facing (which MouseLook rotates)
|
||||
Vector3 move = transform.right * moveX + transform.forward * moveZ;
|
||||
move = Vector3.ClampMagnitude(move, 1f);
|
||||
|
||||
float speed = kb.leftShiftKey.isPressed ? sprintSpeed : moveSpeed;
|
||||
controller.Move(move * speed * Time.deltaTime);
|
||||
|
||||
// Jump
|
||||
if (kb.spaceKey.wasPressedThisFrame && isGrounded)
|
||||
{
|
||||
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
|
||||
}
|
||||
|
||||
// Gravity
|
||||
velocity.y += gravity * Time.deltaTime;
|
||||
controller.Move(velocity * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6780c1c0b9d27c46a0bd43f0d2291f6
|
||||
Reference in New Issue
Block a user