The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

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. :-)

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?

in Engine by (27 points)

Why can't you use rotate_y()?

Because:

  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;

4 Answers

–3 votes

(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 
   Left_b.hide()
   Right_b.hide()

Func _on_right_button_pressed:
   Target_turn += turn_add_amount
   Left_b.hide()
   Rigtht_b.hide()

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
      Left_b.show()
      Right_b.show()

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.

by (111 points)

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

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)
    left_b.hide()
    right_b.hide()
    button_timer.start()

func _on_LeftButton_pressed():
    target_rotation = target_rotation +  Vector3(0,90,0)
    left_b.hide()
    right_b.hide()
    button_timer.start()

func _on_ButtonTimer_timeout():
    left_b.show()
    right_b.show()

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

https://github.com/DamonRaziel/Turning-vehicle-demo

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

I registered just to vote you down.

Cool. Cheers. :)

0 votes

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.

by (29,360 points)
+1 vote

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

by (20 points)
0 votes

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.

by (46 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.