How can I set the rotation of an object in 3d?

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

Hi there, i’m trying to make a 3d third person game, and I need help rotating the character. when I want to go from looking left, to let say up, i want it to set to that rotation. all i can find is just moving it 180 degrees in a certain way, so that it would depend on what direction you would previously be at.

in other words;

starting direction = left

goal = up

what i need to do then is turn the object 90 degrees right.

but what if the starting direction was back?

starting direction = back

goal up

what .i need to do is turn the object 180 degrees.

so its clear that i need to change the amount of degrees turned in order to get the desired result

is there a way, like a function, so that each time, no matter the direction, it will set the rotation to a number of degrees?

thanks

:bust_in_silhouette: Reply From: ovendelon

Sounds like you need to orient your player to a direction vector if I understand you correctly?

You can check Spatial.look_at & Spatial.look_at_from_position in the docs

or you can calculate the angle by yourself:

var angle = transform.basis.z.angle_to( Vector3.up )

where transform.basis.z is the forward of your character and Vector3.up is the desired facing angle


I’ve fond this tutorial which may help you