From 58fac74683a684be318a516405b7ce3ebb474472 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Thu, 21 Mar 2024 20:32:25 +0000 Subject: [PATCH] fix(*): Fix movement for player 2 --- Game/scripts/player2.gd | 45 +++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/Game/scripts/player2.gd b/Game/scripts/player2.gd index 24825ac..c2421ca 100644 --- a/Game/scripts/player2.gd +++ b/Game/scripts/player2.gd @@ -2,17 +2,36 @@ extends 'res://scripts/Vehicle.gd' func get_input_vector() -> Vector3: var input_vector = Vector3.ZERO - if Input.is_action_pressed("move_forward2"): - input_vector.z -= 1 - if Input.is_action_pressed("move_backward2"): - input_vector.z += 1 - if Input.is_action_pressed("move_left2"): - input_vector.x -= 1 - if Input.is_action_pressed("move_right2"): - input_vector.x += 1 - if Input.is_action_pressed("move_up2"): - input_vector.y += 1 - if Input.is_action_pressed("move_down2"): - input_vector.y -= 1 + var camera_path = "/root/MatchNode3D/Players/GridContainer/SubViewportContainer/SubViewport/Player1/Camera3D" + var camera = get_node(camera_path) - return input_vector + if camera: + var camera_transform = camera.global_transform + var forward_direction = -camera_transform.basis.z + var right_direction = camera_transform.basis.x + var y_movement = input_vector.y + + if Input.is_action_pressed("move_forward2"): + input_vector += forward_direction + if Input.is_action_pressed("move_backward2"): + input_vector -= forward_direction + if Input.is_action_pressed("move_left2"): + input_vector -= right_direction + if Input.is_action_pressed("move_right2"): + input_vector += right_direction + + # Zero out the Y component to restrict movement to the XZ plane + input_vector.y = 0 + + # Add back the preserved Y movement + input_vector.y = y_movement + + if Input.is_action_pressed("move_up2"): + input_vector.y += 1 + if Input.is_action_pressed("move_down2"): + input_vector.y -= 1 + + return input_vector.normalized() + else: + print("Camera not found") + return Vector3.ZERO