Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Swagilini |
I’m making a simple Ping Pong game controlled by the mouse. I’ve got all the code worked out for allowing mouse movement and collisions with objects but when I squish the ball into a wall it either clips through the player or the wall.
my code so far
extends KinematicBody2D
export (int, 0, 200) var inertia = 100
var velocity = Vector2.ZERO
func _physics_process(_delta: float) → void:
var direction: Vector2 = position - get_global_mouse_position()
move_and_slide(-direction*10, Vector2.UP, false, 4, PI/4, false)
velocity = move_and_slide(-direction*10)
for index in get_slide_count():
var collision = get_slide_collision(index)
if collision.collider.is_in_group("Ball"):
collision.collider.apply_central_impulse(-collision.normal * velocity.length() * inertia)