Working on my first game, could use advice

Godot Version

v3.5.3

Question

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 :slight_smile:
–bapix

you just need some trigonometry computing.

a is the angle
x is the value of x offset
y is the value of y offset

x=cos(a)
y=sin(a)

the code you write should be almost the same using sin and cos functions in godot:

var angle := 30
var x := cos(angle)
var y := sin(angle)
var vec := Vector2(x, y)

or just

var vec := Vector2(cos(angle), sin(angle))

here you see, you actually can code a full direction moving. and for

you can put it later for you’re new.

and i would like to help you or ”teach“ you directly or something like that, because i’m a Lonely Coder on his Long Trip :neutral_face:

1 Like

two questions, first: what is Vector2?(i get its a function but what does it do?) and second: does this logic work on rigid body 2d?

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!

1 Like

why should i stick with kinematic body?

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.

1 Like

and how can i apply forces?

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?

you can write it in an elegant way:

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.