How to rotate in 3d relative to camera?
Asked Answered
J

2

0

I have a third person character that I move relative to the camera.
But I also want him to look into the direction he is going, but I don't know how.

	var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
	var direction := (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	
	if direction:
                # Attempt 1
		player.look_at(direction, Vector3.UP, true)

                # Attempt 2
		player.rotation.y = lerpf(player.rotation.y, atan2(direction.x, direction.z), 0.3)
		
                # Attempt 3
		var new_transform = player.transform.looking_at(direction, Vector3.UP, true)
		player.transform  = player.transform.interpolate_with(new_transform, 0.9)

                # Attempt 4
		var new_transform = player.transform.looking_at(direction, Vector3.UP, true)
		player.transform  = new_transform

#1, #3 and #4 turn 180 degree and then immediatelly back the first time I start walking. They also have extreme slow rotations.
They probably never reach the wanted rotation.

#2. Works best but at a certain rotation it does a 360 rotation

Is the direction made from the camera.transform the issue?

🤷‍♂️

Javelin answered 7/2 at 21:10 Comment(0)
J
0

Not relevent to my question. I just want to test something that happend by accident in my previous post.

j

j

j

j

j
j
Javelin answered 7/2 at 21:17 Comment(0)
J
1

lerp_angle solves the issue with the 360 rotation.

Now it works:

var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
var direction := (camera.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
              player.rotation.y = lerp_angle(player.rotation.y, atan2(-direction.x, -direction.z), 0.3)
Javelin answered 7/2 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.