hey everybody, im just a complete noob to all of this and im looking for advice on how to write my code, and i hope its not uncommon to ask for this without much done yet.
I’m working on my very first ever godot project and i just got stuck, its a very simple game about an airship fighting against the wind trying to stay stable (maybe later there will be obstacles and a clear objective).
I made the airship scene, added kinematic body 2d, collision shape, sprite, and a script…
-i want the code to handle movement in six different angles: 30°, 90°, 150°, 210°, 270°, 330°.
-i want the movement to last for as long as the key is pressed.
-i don’t want the direction to vary if more than one key is pressed, meaning i want the code to take the initial input from the first key that was pressed and not allow the change of directions again until the key is released.
This is all for now, i just want to get the movement right, although i would also like implementing physics later on, basically id like it if the airship could tilt with the wind and the propellers(the “keys” i mentioned), but i don’t know if i should mention that yet…
if anyone has any ideas they are very much appreciated, thank you all
–bapix
Vector2 is a type, it holds a X and a Y variable, often used to represent position or a direction. RigidBody2D makes use of the physics engine so adding scripts to it should make use of forces, but I think you want to stick with a KinematicBody2D.
Check out the Dodge the Creeps tutorial, I think it has the basics of player movement that you would like to see. Making your ship look in a direction might be easiest with look_at
If any variable, type, or function is new to you I recommend ctrl+clicking on it (or right click “open documentation”) and reading through it on-the-spot!
Kinematic body is easier to modify with scripts. Rigid bodies are largely handled by the physics engine, so you’d apply forces rather than setting position or velocity of the body.
Depends on what you’d like to do, there is apply_impulse/apply_force and the overrideable _integrate_forces function. You can read more about it in the docs
extends RigidBody2D
var movement_force
var up_right := Vector2(cos(30), sin(30))
var up_left := Vector2(cos(150), sin(150))
var up := Vector2(cos(90), sin(90))
var down_left := Vector2(cos(210), sin(210))
var down_right := Vector2(cos(330), sin(330))
var down := Vector2(cos(270), sin(270))
if Input.is_action_pressed(“move_up_right”):
movement_force = up_right
elif Input.is_action_pressed(“move_up_left”):
movement_force = up_left
elif Input.is_action_pressed(“move_up”):
movement_force = up
elif Input.is_action_pressed(“move_down_left”):
movement_force = down_left
elif Input.is_action_pressed(“move_down_right”):
movement_force = down_right
elif Input.is_action_pressed(“move_down”):
movement_force = down
i have the variables and logic from above, (also some keys mapped to actions in the proyect menu) what should i write next?
h_move := Input.get_axis("move_left", "move_right")
v_move := Input.get_axis("move_up", "move_down")
var movement := Vector2(h_move, v_move)
movement.normalize()
get_axis():
In this example, when you press "move_left", it returns -1, when you press "move_right", it returns 1; when you press both of it, you get 0.
in the docs:
“This is a shorthand for writing Input.get_action_strength("positive_action") - Input.get_action_strength("negative_action").”
and you may ignore that…
normalize():
change the vector itself to let it fit the length of 1, but the same time direction remains.
and you can multiply to a number to scale the force.