I'm making an fps game, but the player keeps moving in the same direction when I turn their head.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By zurolg

I’m new to coding, so I have no idea how to fix this. Tutorials aren’t helping, so im pretty mad. Walking on WASD works, but when the head/camera looks the other way, the player keep moving in the same direction! Here is my code.I won’t be surprised if it’s something obvious, but I really need your help! much appreciated.

extends KinematicBody

onready var camera = $head/Camera
const gravity = -30
const speed = 10
const mouse_sensitivity = 0.1 #radiants/pixel

var velicity = Vector3(0,0,0)

func _physics_process(_delta):
if Input.is_action_pressed(“right”):
velicity.x = speed
if Input.is_action_pressed(“left”):
velicity.x = -speed
if Input.is_action_pressed(“forward”):
velicity.z = -speed
if Input.is_action_pressed(“backward”):
velicity.z = speed

velicity = move_and_slide(velicity)

velicity.x = lerp(velicity.x,0,0.3)
velicity.z = lerp(velicity.z,0,0.3)

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
var movement = event.relative
camera.rotation.x += -deg2rad(movement.y * mouse_sensitivity)
camera.rotation.x = clamp(camera.rotation.x, deg2rad(-70), deg2rad(70))
rotation.y += -deg2rad(movement.x * mouse_sensitivity)

:bust_in_silhouette: Reply From: magicalogic

That is because your velocity is always forward.
try putting this line before calling move_and_slide()

velicity = velicity.rotated(rotation)

It will probably mess with the lerping you are doing after calling move_and_slide() so replace those to lines with:

velicity = velicity.linear_interpolate(Vector3.ZERO,0.3)

Thank you so much! I will try this immediately.(May I ask again in case if anythyng goes wrong?thx again)

zurolg | 2021-05-19 02:21

Hello again.I am so sorry to bother you, but I need an advise again. Replacing lerping worked, but the first line disabled the entire code for some reason??? I suspect it may be some godot’s settings or wersion’s fault, but i haven’t seen any tutorials about my problem so maybe i should continue searching how to solve this.

zurolg | 2021-05-19 02:54