Godot Version
4.3
Question
Hello! I’m new to programming and I’m trying to make something similar to the geometry dash flight mode system, and I want the controls to be "press spacebar to rotate counter-clockwise and release spacebar will make the character rotate clockwise until they reach 90 degrees, then they are ‘falling’ but Imput.get_axis requires 2 Imputs. is there a way I can make the character rotate ‘up’ when pressing spacebar and 'fall when it’s released? Thanks in advance to anyone who can help!
extends CharacterBody2D
@export var speed = 400
@export var rotation_speed = 3.5
const GRAVITY = 9.8
var rotation_direction = 0
func get_input():
rotation_direction = Input.get_axis(“fly”,“fall”)
func _physics_process(delta):
get_input()
rotation += rotation_direction * rotation_speed * delta
velocity = transform.x * speed
move_and_slide()
here’s what I got sorry but I don’t know how to post it in the script panel
a single input equivalent to get_axis
is get_action_strength
, but you might want is_action_pressed
|
|
true or false |
Input.is_action_pressed |
one value from zero to one |
Input.get_action_strength |
negative 1 to positive 1 |
Input.get_axis |
2D input with negative and positive x and y axis |
Input.get_vector |
Seems like you have a target rotation in mind at 90°, looking at a video of gameplay I think they prevent the player from going backwards by clamping rotations straight up and straight down. You may need some tricks to handle the fact that rotations wrap around at 360 to 0.
func _physics_process(delta: float) -> void:
# target a downward rotation
var target_rotation: float = PI/2
var pressed: bool = Input.is_action_pressed("fly")
if pressed:
# target upwards if pressed
target_rotation = -PI/2
# move toward the target rotation, may need wrap-around fix
rotation = move_toward(rotation, target_rotation, rotation_speed * delta)
velocity = transform.x * speed
move_and_slide()