3D RigidBody: How to change rotation.y by code?

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


I have a 3D RigidBody with a locked Y-Rotation-Axis (Axis Lock Angular Y). All other linear and angular axes are unlocked.

I want to set the y-rotation by code myself. The translation and rotation of the unlocked axes should be calculated by the physics engine.

I can change rotation.y in _physics_process(delta) or _process(delta), which rotates in y correctly, but this stops the translation and rotation of the unlocked axes, calculated by the physics engine. Another try to change the translation in _integrate_forces(state) doesn’t do anything.

Any ideas?



:bust_in_silhouette: Reply From: klaas

use the RigidBody mode “character”
then modify the transform in the bodystate

func _integrate_forces( state ):
    state.transform.basis = state.transform.basis.rotated(Vector3.UP,the_rotation)

Hi klaas,

state.transform.basis = state.transform.basis.rotated(Vector3.UP,the_rotation)

fixed my problem. I leave the mode as Rigid and not Character, because I need the automatic physics rotation in x, and z.

Thank you so much, I’ve learnd something new, and I think I like Godot more and more, Cheers


MikeMikeMike | 2021-07-10 18:50

Hi i am new to godot and i have done this but after a few rotation my input keys wont work.

ravemaster03 | 2022-09-17 15:44

this issue seems to be related to another problem.

Can you provide code to replicate the issue?

klaas | 2022-09-17 21:25

Thanks so much for the answer! I was struggling with rotating a rigidbody as it wasn’t rotating around its own axis. Your solution works.

Florentin | 2023-01-13 22:22