How to rotate a 3D object on its y axis

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

I know this may sound really basic but I’m a Godot newbie, so feel free to skip this question if it’s too boring for you. :slight_smile:

I have a Spatial node. It has a “car” scene as a child (an imported .dae), and a camera.

Then I have two buttons.

When I press the “Right” button I want the car to rotate right (90 degrees).
When I press the “Left” button i want the car to rotate left (90 degrees).

During the rotation process, the buttons should be disabled (and enabled again when the rotation animation terminates).

I found I should not use rotate_y, but I can’t find an example of 3D transforms that I can eventually adapt.

Can you help me?

Why can’t you use rotate_y()?

Zylann | 2019-07-05 16:39


  1. if I use get_node("car").rotate_y(deg2rad(90.0)) I get an instant rotation, without any animation;

  2. I red somewhere in the docs it is better to use 3DTransforms;

OTGOD | 2019-07-06 07:20

:bust_in_silhouette: Reply From: DamonR

(From my phone)

Have you studied the 3d character selection demo available as a template?
It smoothly rotates between points, I think it uses interpolation to achieve the slide between points.

Or you could try something like:

Var target_turn = 0
Var turn_add_amount = 90
Var turn_incrememt  = 1
Var left_b = get_node("left_button")
Var right_b = get_node("right_button")

Func _on_left_button_pressed():
   Target_turn -= turn_add_amount 

Func _on_right_button_pressed:
   Target_turn += turn_add_amount

Func _process(delta):
   If target_turn < self.global_transform().origin.y:
      Self.global_transform().origin.y -= turn_increment
   Elif target_turn > self.global_transform().origin.y:
      Self.global_transform().origin.y += turn_incrememt 
   Elif target_turn == self.global_transform().origin.y

That should rotate it around the targets global y axis. Hiding and showing the buttons will disable/enable them.

I have not tested this yet, but will when I am on my computer later.

Hope this helps.

I was thinking of basis, not origin, origin is for movement in a direction.

DamonR | 2019-07-06 18:41

extends KinematicBody

onready var left_b = get_node("Camera/UIControl/LeftButton")
onready var right_b = get_node("Camera/UIControl/RightButton")
onready var button_timer = get_node("Camera/UIControl/ButtonTimer")
var current_angle = Vector3();
var target_rotation = Vector3();
export var speed = 3.0;

func _process(delta):
	rotation_degrees = Vector3(0,lerp(rotation_degrees.y,target_rotation.y,speed*delta),0)

func _on_RightButton_pressed():
	target_rotation = target_rotation -  Vector3(0,90,0)

func _on_LeftButton_pressed():
	target_rotation = target_rotation +  Vector3(0,90,0)

func _on_ButtonTimer_timeout():

Modified code that works. Made a quick demo scene that I will attach shortly.

DamonR | 2019-07-06 20:22

GitHub - DamonRaziel/Turning-vehicle-demo

A single scene with node setup and code mentioned above. I hope it helps if you still need it.

DamonR | 2019-07-06 20:27

I registered just to vote you down.

georgewasintong | 2023-06-29 13:51

Cool. Cheers. :slight_smile:

DamonR | 2023-06-29 14:46

:bust_in_silhouette: Reply From: Zylann

If you want an animation, then you need to change the rotation property AND write some code that animates it (i.e increments it at regular intervals over time to produce that animation). For that you could use a Tween or AnimationPlayer, or do it with code like DamonR suggested.

:bust_in_silhouette: Reply From: blink

You can use get_node(“car”).rotate_y(deg2rad(90.0)) but instead of 90 put 1 degree so the rotation will be slower also that will give it an animation effect

:bust_in_silhouette: Reply From: Ingeniou5

If someone needs a simple solution to rotate an object - I found this:

get_node("Node Name Here").rotate(Vector3(0,1,0),0)

if you want to ADD rotation, just change the last zero. Vector3(0,1,0) represents rotation by Y axis, (1,0,0) is for X and (0,0,1) is for Z, I think.

get_node("Node Name Here").set_rotation(Vector3(0,0,0))

and this is how you can SET rotation.

It all seems to work with PI, not degrees. So instead of 90 45 30 there should be PI/2, PI/4, PI/6 etc.