Godot Version
4.3
Question
Hello! I am an aspiring Godot game developer making a pong-like game as a small project, while I study GDScript and the engine. However, I’ve faced a problem, which is driving me crazy. My ball (CharacterBody2D) doesn’t bounce on collision. Instead, it starts jittering. Here’s the code btw:
extends CharacterBody2D
func LaunchBall(moving_direction, speed, delta):
velocity = moving_direction * speed * delta
var collision = move_and_collide(velocity)
return collision
func BounceBall(rebound_direction, speed, delta):
var bounce_velocity = rebound_direction * speed * delta
move_and_collide(bounce_velocity)
func _process(delta):
if LaunchBall(Vector2(-50,10), 5, delta):
BounceBall(Vector2(-50, 10) * -1, 5, delta)
I think I understand what is causing the problem:
-
Upon receiving the CollisionObject2D, the if-statement returns true and it calls the BounceBall function with appropriate arguments.
-
Variable bounce_velocity gets it’s value from passed arguments.
-
Function move_and_collide gets called with parameter bounce_velocity → ball tries to move into different direction, but cannot since the _process function gets called again.
-
Jittering starts
Honestly, i think my understanding of GDScript or the engine isn’t just good enough, which is why i am asking from here.
Thanks