Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | imekon | |
Old Version | Published before Godot 3 was released. |
The following used to work with V2.0 but no longer in V3.0:
apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(rotation))
It does the acceleration OK but the rotation seems to be locked straight up. I can no longer rotate and point where I want to go.
Here’s the full code:
extends RigidBody2D
const ROTATE_SPEED = 250
const ACCELERATION = 5
func _physics_process(delta):
if Input.is_action_pressed("ui_left"):
rotate(-deg2rad(delta * ROTATE_SPEED))
if Input.is_action_pressed("ui_right"):
rotate(deg2rad(delta * ROTATE_SPEED))
if Input.is_action_pressed("ui_up"):
apply_impulse(Vector2(), Vector2(0, -ACCELERATION).rotated(rotation))
If I don’t press UP, the LEFT and RIGHT will rotate the ship as expected. However, press UP and it locks going straight up, jiggling slightly if I press LEFT or RIGHT.
How do I make this work in V3.0?
It looks as though apply_impulse is resetting rotation. I changed the code to this:
if Input.is_action_pressed("ui_up"):
print("Ship rotation: " + str(rotation))
apply_impulse(Vector2(), Vector2(0, ACCELERATION).rotated(rotation))
print("Ship rotation: " + str(rotation))
I get this printout:
Ship rotation: -0.727221
Ship rotation: -0.727221
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
Ship rotation: 0
imekon | 2017-12-24 09:51