49 lines
1.6 KiB
C#
49 lines
1.6 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace IllOmen
|
|
{
|
|
/// <summary>Free walking for inspecting test scenes: WASD to move, Shift to hurry, mouse to look.</summary>
|
|
public class WalkAround : MonoBehaviour
|
|
{
|
|
[SerializeField] private float walkSpeed = 1.5f;
|
|
[SerializeField] private float runSpeed = 3.5f;
|
|
[SerializeField] private float sensitivity = 0.08f;
|
|
[SerializeField] private float maxPitch = 80f;
|
|
|
|
private float yaw;
|
|
private float pitch;
|
|
|
|
private void Start()
|
|
{
|
|
yaw = transform.eulerAngles.y;
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Mouse mouse = Mouse.current;
|
|
if (mouse != null)
|
|
{
|
|
Vector2 delta = mouse.delta.ReadValue() * sensitivity;
|
|
yaw += delta.x;
|
|
pitch = Mathf.Clamp(pitch - delta.y, -maxPitch, maxPitch);
|
|
transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
|
|
}
|
|
|
|
Keyboard keyboard = Keyboard.current;
|
|
if (keyboard == null) return;
|
|
|
|
Vector3 input = Vector3.zero;
|
|
if (keyboard.wKey.isPressed) input.z += 1f;
|
|
if (keyboard.sKey.isPressed) input.z -= 1f;
|
|
if (keyboard.dKey.isPressed) input.x += 1f;
|
|
if (keyboard.aKey.isPressed) input.x -= 1f;
|
|
|
|
float speed = keyboard.leftShiftKey.isPressed ? runSpeed : walkSpeed;
|
|
transform.position += Quaternion.Euler(0f, yaw, 0f) * input.normalized * speed * Time.deltaTime;
|
|
}
|
|
}
|
|
}
|