Added WASD Movement
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using Mirror;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class CharacterMovement : NetworkBehaviour
|
||||
{
|
||||
@@ -11,6 +12,7 @@ public class CharacterMovement : NetworkBehaviour
|
||||
PlayerInput input;
|
||||
|
||||
Vector2 currentMovement;
|
||||
Vector2 combinedMovement;
|
||||
bool movementPressed;
|
||||
bool runPressed;
|
||||
|
||||
@@ -27,7 +29,14 @@ public class CharacterMovement : NetworkBehaviour
|
||||
currentMovement = ctx.ReadValue<Vector2>();
|
||||
movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
|
||||
};
|
||||
input.CharacterControls.Movement.canceled += ctx =>
|
||||
{
|
||||
currentMovement = Vector2.zero;
|
||||
movementPressed = false;
|
||||
};
|
||||
|
||||
input.CharacterControls.Run.performed += ctx => runPressed = ctx.ReadValueAsButton();
|
||||
input.CharacterControls.Run.canceled += ctx => runPressed = false;
|
||||
}
|
||||
|
||||
void Start()
|
||||
@@ -50,13 +59,38 @@ public class CharacterMovement : NetworkBehaviour
|
||||
void HandleRotation()
|
||||
{
|
||||
Vector3 currentPosition = transform.position;
|
||||
Vector3 newPosition = new Vector3(currentMovement.x, 0, currentMovement.y);
|
||||
Vector3 positionToLookAt = currentPosition + newPosition;
|
||||
transform.LookAt(positionToLookAt);
|
||||
Vector3 newPosition = new Vector3(combinedMovement.x, 0, combinedMovement.y);
|
||||
// only rotate when there is movement to look at
|
||||
if (newPosition.sqrMagnitude > 0f)
|
||||
{
|
||||
Vector3 positionToLookAt = currentPosition + newPosition;
|
||||
transform.LookAt(positionToLookAt);
|
||||
}
|
||||
}
|
||||
|
||||
void HandleMovement()
|
||||
{
|
||||
// read keyboard WASD (supplements / works alongside Input System action bindings)
|
||||
Vector2 keyboardMovement = Vector2.zero;
|
||||
var kb = Keyboard.current;
|
||||
if (kb != null)
|
||||
{
|
||||
if (kb.wKey.isPressed) keyboardMovement.y += 1f;
|
||||
if (kb.sKey.isPressed) keyboardMovement.y -= 1f;
|
||||
if (kb.aKey.isPressed) keyboardMovement.x -= 1f;
|
||||
if (kb.dKey.isPressed) keyboardMovement.x += 1f;
|
||||
}
|
||||
|
||||
// combine input action movement and keyboard movement
|
||||
combinedMovement = currentMovement + keyboardMovement;
|
||||
// prevent faster diagonal movement
|
||||
combinedMovement = Vector2.ClampMagnitude(combinedMovement, 1f);
|
||||
movementPressed = combinedMovement.x != 0 || combinedMovement.y != 0;
|
||||
|
||||
// detect Shift keys as an additional run input
|
||||
bool shiftPressed = kb != null && (kb.leftShiftKey.isPressed || kb.rightShiftKey.isPressed);
|
||||
bool runActive = runPressed || shiftPressed;
|
||||
|
||||
bool isRunning = animator.GetBool(isRunningHash);
|
||||
bool isWalking = animator.GetBool(isWalkingHash);
|
||||
|
||||
@@ -72,13 +106,13 @@ public class CharacterMovement : NetworkBehaviour
|
||||
isWalking = false;
|
||||
}
|
||||
|
||||
if ((movementPressed && runPressed) && !isRunning)
|
||||
if ((movementPressed && runActive) && !isRunning)
|
||||
{
|
||||
animator.SetBool(isRunningHash, true);
|
||||
isRunning = true;
|
||||
}
|
||||
|
||||
if ((!movementPressed || !runPressed) && isRunning)
|
||||
if ((!movementPressed || !runActive) && isRunning)
|
||||
{
|
||||
animator.SetBool(isRunningHash, false);
|
||||
isRunning = false;
|
||||
@@ -93,10 +127,8 @@ public class CharacterMovement : NetworkBehaviour
|
||||
}
|
||||
|
||||
// movement translation (local)
|
||||
// keep your movement application here (velocity, CharacterController, Rigidbody.ApplyForce, etc.)
|
||||
// Example simple translation (adjust speed as needed):
|
||||
float speed = isRunning ? 6f : 3f;
|
||||
Vector3 move = new Vector3(currentMovement.x, 0, currentMovement.y) * speed * Time.deltaTime;
|
||||
Vector3 move = new Vector3(combinedMovement.x, 0, combinedMovement.y) * speed * Time.deltaTime;
|
||||
transform.position += move;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
2606
Assets/_Recovery/0 (1).unity
Normal file
2606
Assets/_Recovery/0 (1).unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/_Recovery/0 (1).unity.meta
Normal file
7
Assets/_Recovery/0 (1).unity.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: daf41725d2fb476419fe1e956b6cbc77
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user