Added WASD Movement

This commit is contained in:
pelpanagiotis
2026-01-16 12:16:18 +02:00
parent acd7255a88
commit fb5db5d350
21 changed files with 20880 additions and 12724 deletions

View File

@@ -50,20 +50,72 @@ namespace QuickStart
playerColor = _col;
}
/*
PSEUDOCODE / PLAN (detailed):
1. If this is not the local player:
- Keep existing behavior: make the floatingInfo look at the main camera every frame.
- Return early so non-local players do not process input or move.
2. If this is the local player:
- Read keyboard input from the Unity Input System (preferred):
- Use UnityEngine.InputSystem.Keyboard.current to check key states.
- Map WASD (and arrow keys as optional) to movement:
- W or UpArrow -> move forward (+z)
- S or DownArrow -> move backward (-z)
- A or LeftArrow -> rotate left (-y)
- D or RightArrow -> rotate right (+y)
- Movement magnitudes:
- Rotation speed: 110 degrees per second (same as original)
- Forward/back speed: 4 units per second (same as original)
- Multiply by Time.deltaTime for frame-rate independence.
- If the Input System is not available (Keyboard.current == null), fall back to the legacy Input.GetAxis calls to preserve behavior on setups that haven't enabled the new system.
- Apply transform.Rotate for yaw (y-axis) using computed rotation value.
- Apply transform.Translate for forward/back movement in local Z using computed move value.
3. Keep the method minimal and efficient:
- Avoid allocations in Update.
- Use direct boolean checks (isPressed) on the keyboard.
- Preserve original movement multipliers and semantics.
*/
void Update()
{
Debug.Log("Update");
if (!isLocalPlayer)
{
// make non-local players run this
floatingInfo.transform.LookAt(Camera.main.transform);
if (Camera.main != null)
floatingInfo.transform.LookAt(Camera.main.transform);
return;
}
float moveX = Input.GetAxis("Horizontal") * Time.deltaTime * 110.0f;
float moveZ = Input.GetAxis("Vertical") * Time.deltaTime * 4f;
float rotationDelta = 0f;
float forwardDelta = 0f;
transform.Rotate(0, moveX, 0);
transform.Translate(0, 0, moveZ);
var kb = UnityEngine.InputSystem.Keyboard.current;
if (kb != null)
{
// Rotation: A/D or Left/Right arrows
if (kb.aKey.isPressed || kb.leftArrowKey.isPressed) rotationDelta = -1f;
if (kb.dKey.isPressed || kb.rightArrowKey.isPressed) rotationDelta = 1f;
// Forward/Backward: W/S or Up/Down arrows
if (kb.wKey.isPressed || kb.upArrowKey.isPressed) forwardDelta = 1f;
if (kb.sKey.isPressed || kb.downArrowKey.isPressed) forwardDelta = -1f;
rotationDelta *= Time.deltaTime * 110.0f; // degrees per second
forwardDelta *= Time.deltaTime * 4.0f; // units per second
}
else
{
// Fallback to legacy input if the new Input System isn't available
rotationDelta = Input.GetAxis("Horizontal") * Time.deltaTime * 110.0f;
forwardDelta = Input.GetAxis("Vertical") * Time.deltaTime * 4.0f;
}
transform.Rotate(0f, rotationDelta, 0f, Space.Self);
transform.Translate(0f, 0f, forwardDelta, Space.Self);
}
}
}