Topic was automatically imported from the old Question2Answer platform.
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.
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
Because:
if I use get_node("car").rotate_y(deg2rad(90.0)) I get an instant rotation, without any animation;
I red somewhere in the docs it is better to use 3DTransforms;
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.
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)
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.
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.
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