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

@@ -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;
}