You can just add multiple different vectors to the motion variable, which should get you something close to what I think you are looking for. For example, I think the following code should do something similar to what you are looking for:
extends KinematicBody2D
# The velocity/motion of the KinematicBody2D node
var motion = Vector2(0, 0)
# The strength of the force applied by each key
const A_KEY_FORCE = 10
const B_KEY_FORCE = 20
const C_KEY_FORCE = 10
func _ready():
pass
func _physics_process(delta):
# Make a vector to store the direction the input will cause the character to move in
var input_motion = Vector2(0, 0)
if (Input.is_action_pressed("A_Key"):
# Add right movement to input_motion
input_motion += Vector2(1, 0) * A_KEY_FORCE
if (Input.is_action_pressed("B_Key"):
# Add Upwards motion to input_motion
input_motion += Vector2(0, -1) * B_KEY_FORCE
if (Input.is_action_pressed("C_Key"):
# Add a slanted left-up motion to input_motion
input_motion += Vector2(-0.5, -0.5) * C_KEY_FORCE
# Optional: clamp the motion within a maximum speed of 40 units
if (input_motion.length() > 40):
input_motion = input_motion.normalized() * 40
# Set motion to input_motion
# (you can also add input_motion to motion, depending on how you want the character to move)
motion = input_motion
# Move the player using move_and_slide
motion = move_and_slide(motion)
I have not tested the code and it is very bare bones, but that is one way you could implement the code where it changes the motion variable based on the input(s) pressed.
If you are adding input_motion to motion, you'll probably want to add some form of drag so when no keys are pressed the character slows down, as otherwise the character will never decelerate.
Hopefully the code above makes sense. The main idea is to add different values to motion (or input_motion in this case) so the movement effects are combined together when the character moves using move_and_slide.
@Pechvogel said:
Maybe I can try to use Rigidbodies. Is it bad practise to let the colliders touch each other the whole time? Since they are pushing the character... Any performance issues?
You could do it that way, though if you can help it I would not recommend using RigidBody2D nodes to move a character, if nothing else because of the complexities it would bring.
One of the biggest issues I can see is the RigidBody2D nodes moving out of alignment and therefor no longer pushing the character. Because RigidBody2D nodes can can be effected by other RigidBody2D nodes (and KinematicBody2D nodes if I recall correctly), you will need to write code to keep them in the correct relative positions while pushing the character in the indented direction, which may be tricky to get working.
Another issue is having the KinematicBody2D node effected by the RigidBody2D nodes movement. Last I knew, KinematicBody2D nodes can push RigidBody2D nodes, but not the other way around. If this is the case, the having the RigidBody2D nodes move the character might require writing code that transfers the velocity of the impact with a RigidBody2D node to the KinematicBody2D node.
That said, I might be mistaken and/or things have changed, in which case this would not be nearly as much of an issue.
I'm not sure if there would be performance issues or not. For a few RigidBody2D nodes, it probably will not be an issue even if there is collision every frame, so long as there are not too many collisions going on in the physics world.
It will almost certainly have some performance implications, but how much I have no idea. I mostly do 3D game development with Godot, so I'm not as familiar with the technicalities of the 2D side.
On the 3D side, a few dozen RigidBody nodes colliding at the same time does not impact performance too much, especially if the collision shapes are relatively simple. I imagine this holds for RigidBody2D nodes as well.
The best way to know for sure would be to test and see what the performance looks like.
Hopefully this helps!
(Side note: Welcome to the forums!)