0 votes

Hey, so I want that when I press the right arrow button, my player a rectangle (Area2D) rotates by 90 degrees. If the left arrow button is pressed, the player should rotate by -90 degrees. However, it should exactly turn to 90 180 270 360 450 ... degrees, otherwise the rectangle will not have straight lines.
Currently I have:

func _process(delta):
print($Player.rotation)
if rotate_to > $Player.get_rotation_degrees():
    $Player.rotate((1 * delta)) ##### $Player.rotate((1 * delta) * speed)
    abc = true
elif abc == true:
    abc = false
    $Player.set_rotation_degrees(int($Player.get_rotation_degrees())) 
elif $Player.get_rotation_degrees() >= 360.0:
    $Player.set_rotation_degrees(0)
    rotate_to = 0
print($Player.get_rotation_degrees())
func _input(event):
if Input.is_key_pressed(KEY_RIGHT) and not event.is_echo():
    rotate_to += 90

This works fine, but when i want to add a speed var, to make it faster, it breaks.

in Projects by (107 points)

1 Answer

+1 vote
Best answer

The way your code is currently set up, when the rectangle has finished rotating it will round the rotation value down so it cannot be a decimal number (int rounds down).

What is happening is that the final rotation is still a decimal but it is also greater than a multiple of 90 (e.g. 181.437, 272.153) so it rounds down but not to a multiple of 90 like what you want but instead to a number like 181 or 272.

If you were to remove this code:

elif $Player.get_rotation_degrees() >= 360.0:
    $Player.set_rotation_degrees(0)
    rotate_to = 0

then the effect would be much more noticeable because it would start to stack instead of being reset after every full cycle.

If you wish to keep most of the current logic that you have now then you can use this to fix your problem:

func _process(delta):
    print($Player.rotation)
    if rotate_to > $Player.get_rotation_degrees():
        $Player.rotate((1 * delta)) ##### $Player.rotate((1 * delta) * speed)
        abc = true
        if ($Player.get_rotation_degrees() > rotate_to):
            $Player.set_rotation_degrees(rotate_to)
    elif abc == true:
        abc = false
        $Player.set_rotation_degrees(int($Player.get_rotation_degrees())) 
    elif $Player.get_rotation_degrees() >= 360.0:
        $Player.set_rotation_degrees(0)
        rotate_to = 0
    print($Player.get_rotation_degrees())

func _input(event):
    if Input.is_key_pressed(KEY_RIGHT) and not event.is_echo():
    rotate_to += 90

However, I feel like this approach is a lot simpler:

func _process(delta):
    $Player.rotation_degrees = move_toward($Player.rotation_degrees, rotate_to, delta * 100 * speed)

func _input(event):
    if Input.is_key_pressed(KEY_RIGHT) and not event.is_echo():
        rotate_to += 90

Note that I added delta * 100 * speed instead of just delta * speed because I'm presuming speed is supposed to be used like a multiplier and if speed was at 1x it would be extremely slow.

by (156 points)
selected 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 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 webmaster@godotengine.org with your username.

Categories