-
Notifications
You must be signed in to change notification settings - Fork 2
add EditorPlayerController closes #97 #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,103 @@ | ||||||
| using UnityEngine; | ||||||
| using UnityEngine.InputSystem; | ||||||
|
|
||||||
| namespace Styly.XRRig | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// Controller for operating local avatar in the editor | ||||||
| /// </summary> | ||||||
| #if UNITY_EDITOR | ||||||
| public class EditorPlayerController : MonoBehaviour | ||||||
| { | ||||||
| [Header("Target")] [SerializeField] private Transform target; | ||||||
|
|
||||||
| [Header("Movement Settings")] [SerializeField] | ||||||
| private float moveSpeed = 5f; | ||||||
|
|
||||||
| [Header("Look Settings")] [SerializeField] | ||||||
| private float lookSensitivity = 8f; | ||||||
|
|
||||||
| private Transform controlTarget; | ||||||
| private float rotationX = 0f; | ||||||
| private float rotationY = 0f; | ||||||
|
|
||||||
| void Start() | ||||||
| { | ||||||
| // Set control target (use self if target is null) | ||||||
| controlTarget = target != null ? target : transform; | ||||||
|
|
||||||
| // Preserve current rotation | ||||||
| rotationY = controlTarget.eulerAngles.y; | ||||||
| rotationX = controlTarget.eulerAngles.x; | ||||||
| } | ||||||
|
|
||||||
| void Update() | ||||||
| { | ||||||
| HandleMovement(); | ||||||
| HandleRotation(); | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Movement handling with WASD (XZ plane) and EQ (Y axis) | ||||||
| /// </summary> | ||||||
| private void HandleMovement() | ||||||
| { | ||||||
| Vector3 moveDirection = Vector3.zero; | ||||||
| Keyboard keyboard = Keyboard.current; | ||||||
|
|
||||||
| if (keyboard == null) return; | ||||||
|
|
||||||
| // XZ plane movement (WASD) | ||||||
| if (keyboard.wKey.isPressed) | ||||||
| moveDirection += controlTarget.forward; | ||||||
| if (keyboard.sKey.isPressed) | ||||||
| moveDirection -= controlTarget.forward; | ||||||
| if (keyboard.aKey.isPressed) | ||||||
| moveDirection -= controlTarget.right; | ||||||
| if (keyboard.dKey.isPressed) | ||||||
| moveDirection += controlTarget.right; | ||||||
|
|
||||||
| // Y axis movement (E/Q) | ||||||
| if (keyboard.eKey.isPressed) | ||||||
| moveDirection += Vector3.up; | ||||||
| if (keyboard.qKey.isPressed) | ||||||
| moveDirection -= Vector3.up; | ||||||
|
|
||||||
| // Apply movement | ||||||
| if (moveDirection.magnitude > 0) | ||||||
| { | ||||||
| moveDirection.Normalize(); | ||||||
| controlTarget.position += moveDirection * moveSpeed * Time.deltaTime; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// View rotation handling with right mouse button drag | ||||||
| /// </summary> | ||||||
| private void HandleRotation() | ||||||
| { | ||||||
| Mouse mouse = Mouse.current; | ||||||
| if (mouse == null) return; | ||||||
|
|
||||||
| // Rotate only while right-clicking | ||||||
| if (mouse.rightButton.isPressed) | ||||||
| { | ||||||
| Vector2 mouseDelta = mouse.delta.ReadValue() * lookSensitivity * 0.1f; | ||||||
|
|
||||||
| rotationY += mouseDelta.x; | ||||||
| rotationX -= mouseDelta.y; | ||||||
|
|
||||||
| // Vertical angle limit | ||||||
| rotationX = Mathf.Clamp(rotationX, -90f, 90f); | ||||||
|
||||||
| rotationX = Mathf.Clamp(rotationX, -90f, 90f); | |
| rotationX = Mathf.Clamp(rotationX, minVerticalAngle, maxVerticalAngle); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The magic number 0.1f should be extracted to a named constant or configurable field to make the sensitivity scaling more transparent and maintainable.