PhysicsProgrammingHelp
As the title says, I need help on how to make a 2D wheel rotate.
I’m making a game and what I need is to make the wheel rotate based on the buttons I click, but I’m new to game making, and I don’t really know how to do these things.
This is the current code:
extends RigidBody2D
var rotationSpeed:float = 1.0
func _process(_delta: float) -> void:
global_position = get_parent().get_node("block").global_position
func _input(event: InputEvent) -> void:
if event is InputEventKey:
if Input.is_action_pressed("left") and not Input.is_action_pressed("right"):
global_rotation_degrees -= rotationSpeed
elif Input.is_action_pressed("right") and not Input.is_action_pressed("left"):
global_rotation_degrees += rotationSpeed
And this is my scene
At the moment the wheel kinda rotates, but it keeps snapping and after releasing it just starts rotating randomly.
But, since your using is_action_pressed(). To my knowledge, thats like Input.is_action_just_pressed(), so you might want to use is_action() to make the code run while your holding the button
@onready var Wheel = $Wheel
var rotation_speed: float = 1.0
var rotation_direction: int = 1
var is_rotating: bool = false
func _input(event):
if event.is_action_pressed("ui_left"):
rotation_direction = -1
is_rotating = true
elif event.is_action_released("ui_left"):
is_rotating = false
if event.is_action_pressed("ui_right"):
is_rotating = true
rotation_direction = 1
elif event.is_action_released("ui_right"):
is_rotating = false
func _process(delta):
if is_rotating:
Wheel.rotation += (rotation_direction * rotation_speed * delta)
The is_action_pressed only fires when the button is pressed, but you can detect when it is released too. You can use that to switch the flag for is_rotating on and off, and change the direction on left and right. You can then deal with the rotation in the process function.