From 60f2797541dd055e66d4d5cbd1f936aa1ed6f059 Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 27 Jan 2025 17:30:45 +0100 Subject: [PATCH] Added strafing --- Scripts/PlayerMovement.cs | 34 ++++++++++++++++++++++++++++++++-- project.godot | 30 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index bcce0634..c84c7324 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -29,6 +29,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private Vector2 _movementDirection { get; set; } private Vector2 _facingDirection { get; set; } + + private Vector2 _rightStickInput { get; set; } private Sprite2D _crosshair; @@ -42,6 +44,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private GameManager _gameManager; + private bool _isStrafing { get; set; } + //private InventoryManager _inventoryManager; public override void _Ready() @@ -54,6 +58,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible _movementDirection = Vector2.Zero; _facingDirection = Vector2.Zero; + _rightStickInput = Vector2.Zero; + _isStrafing = false; _gameManager = GetNode("/root/GameScene"); @@ -148,6 +154,14 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible { return Input.GetVector("left", "right", "up", "down"); } + + private Vector2 GetRightStickInput() + { + return new Vector2( + Input.GetAxis("aim_left","aim_right"), + Input.GetAxis("aim_up", "aim_down") + ); + } private Vector2 CalculateCrosshairPosition() { @@ -165,10 +179,26 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible public override void _PhysicsProcess(double delta) { _movementDirection = GetInput(); - if (_movementDirection != Vector2.Zero) + _isStrafing = Input.IsActionPressed("strafe"); + _rightStickInput = GetRightStickInput(); + + // Update Facing Direction + if (!_isStrafing) { - _facingDirection = _movementDirection; + if (_rightStickInput.Length() > 0.1f) // If the right stick is moved + { + _facingDirection = _rightStickInput.Normalized(); + } + else if (_movementDirection != Vector2.Zero) // Fall back to movement direction + { + _facingDirection = _movementDirection; + } } + + // if (_movementDirection != Vector2.Zero) + // { + // _facingDirection = _movementDirection; + // } Velocity = _movementDirection * (float)(Speed * delta); MoveAndSlide(); diff --git a/project.godot b/project.godot index 18a61e44..0d42b3e3 100644 --- a/project.godot +++ b/project.godot @@ -88,6 +88,36 @@ Use={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":70,"key_label":0,"unicode":102,"location":0,"echo":false,"script":null) ] } +aim_right={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194444,"key_label":0,"unicode":54,"location":0,"echo":false,"script":null) +] +} +aim_left={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":-1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194442,"key_label":0,"unicode":52,"location":0,"echo":false,"script":null) +] +} +aim_up={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":-1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194446,"key_label":0,"unicode":56,"location":0,"echo":false,"script":null) +] +} +aim_down={ +"deadzone": 0.5, +"events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":3,"axis_value":1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194440,"key_label":0,"unicode":50,"location":0,"echo":false,"script":null) +] +} +strafe={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":10,"pressure":0.0,"pressed":false,"script":null) +] +} [layer_names]