0 votes

Hi guys. I've been trying to make a ball bounce with vector math (I know I could just look at the cases where it bounces of a wall and of the ground independently but where's the fun in that amirite).

The first bounce is working fine, but when it hits the ground for the 2nd time I get a null instance error, getslidecollision(0) doesn't exist.

Any ideas why that's happening?

extends KinematicBody2D

const gravity = 10
const bounce_force = 700
const friction = 0.047
const friction_ratio = 75

# do not change ----------------
var sprite_rotation_speed = friction/friction_ratio
#               ----------------

var motion = Vector2(0,0)
var normal = Vector2(0,-1)

var gravity_multiplier = 1
var rotation_force = 0

func _physics_process(delta):

    motion.y += gravity * gravity_multiplier
    if test_move(global_transform, delta*motion):
        var aux_motion = motion;

        motion = move_and_slide(motion, normal)

        motion = aux_motion.bounce(get_slide_collision(0).normal).normalized()*lerp(bounce_force, 0, motion.angle_to(aux_motion)/PI)

        motion = move_and_slide(motion, normal)
    if get_slide_count() > 0:
in Engine by (16 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.