#Trying to make a bouncing ball, but can't get any code I've found to work

3 messages · Page 1 of 1 (latest)

jovial crow
#

Hi so I'm pretty new to Godot. I'm trying to make a ball bounce off walls and paddles in a sorta pong clone (for learning). I've hit a big snag. I found code that people use to make bounces happen but I just get "Invalid get index 'normal' (on base: 'KinematicCollision2D')."

This is the code:
`extends RigidBody2D

var velos = (Vector2(200,200))

func _physics_process(delta):
var col = move_and_collide(velos)
if (col):
velos = velos.bounce(col.normal)`

I borrowed it from another person in the help forum as it was close to the Godot 3 code I found, but updated for Godot 4. However I cannot figure out how to get the the game to not instantly crash. I've tried using a CharacterBody2D as well as a RigidBody2D.

#

The original Godot 3 code I tried was:
`var velocity = Vector2(200,200)

func _physics_process(delta):

var collision_info = move_and_collide(velocity * delta)
if collision_info:
velocity = velocity.bounce(collision_info.normal)`

#

I got an immediate error for using veloicty, but regardless of what I set it seems to hate the .normal