• 3D
  • Rotation of mesh

I want to rotate a mesh in the direction that the player is going. I found a good tutorial by Jayanam and the code I use is based on it. ''' if isMoving: mesh.rotation.y = atan2(velocity.x, velocity.z) ''' It works but the rotation is not smooth. Some tips on how you rotate player characters would help me a lot.

One idea is to do something like this.

const ROTATION_SPEED := 1.5
const ROTATION_DAMP := 3.0
const DAMP_CLAMP := 0.005

var spin := 0.0


func _process(delta):
	_rotation(delta)


func _rotation(delta) -> void:
	var rotation_right = Input.is_action_pressed("rotation_right")
	var rotation_left = Input.is_action_pressed("rotation_left")

	if rotation_right and rotation_left:
		spin = 0.0
	elif rotation_right:
		spin = 1.0
	elif rotation_left:
		spin = -1.0
	elif abs(spin) > DAMP_CLAMP:
		spin = lerp(spin, 0.0, ROTATION_DAMP * delta)
	else:
		spin = 0.0

	rotate_y(spin * ROTATION_SPEED * delta)

That's for keyboard controlled rotation but I think you can see what it's doing. It's also got clamping which you might not need, you could just have your rotation happening constantly if that fits better with the game.

@AlexandrosKap I've changed the topic into a question so you can mark @Kequc 's post as an answer. For future reference instead of clicking on New Discussion if you click on the arrow on the right side of the button you get a drop down from where you can chose to aska question instead.