简单的玩家预制体和脚本
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user