How to check if player is moving to the side?

Godot Version

4.3

Question

I want to make a script that rotates the cam by z if player is moving to the side like in Half-Life 1

It heavily depends on how your player object is structured / set up, but normally I’d just check the player’s Velocity, that’s probably the easiest method to do so.

as for the movement structure it is the standard script

var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		is_moving = true
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		is_moving = false
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

I’ve tried to use velocity, but if I try to turn the cam by y, the cam will only turn by z if I go forward, which is wrong

Try something like this:

var MAX_CAMERA_TILT = deg_to_rad(10)

var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()

# picked a random number 3 for how quickly the effect settles
camera.rotation.z = move_toward(camera.rotation.z, direction.x * MAX_CAMERA_TILT, 3 * delta)
1 Like

ty but still doesn’t work : (

Can you explain what about it doesn’t work? I’d also like to know how this sample is suppose to interact with the camera:

1 Like


Cam script:

func _input(e) -> void:
	if e is InputEventMouseMotion and Params.mouse_input:
		rot_y -= e.relative.x * ROT
		rot_x -= e.relative.y * ROT


		if rot_x < -PI/2: rot_x = -PI/2
		elif rot_x > PI/2: rot_x = PI/2


		transform.basis = Basis(Vector3(0,1,0), rot_y)
		Cam.transform.basis = Basis(Vector3(1,0,0), rot_x)
1 Like

Thank you, the video was informative. I think using input_dir instead will be more effective, my bad.

camera.rotation.z = move_toward(camera.rotation.z, input_dir.x * MAX_CAMERA_TILT, 1 * delta)
1 Like

ty : D

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.