0 votes

I am following this tutorial to make a KinematicBody2D align to the surfaces below it.

When switching from one surface to another, the character bounces until it corrects/aligns its rotation, as shown here:

animation showing character bouncing

How can I eliminate the bouncing?

I've uploaded a minimal version of the project on GitHub.

The project setup looks like this:
Godot editor screenshot

And this is the player script:

extends KinematicBody2D

export (int) var speed = 600
export (int) var jump_speed = -600
export (int) var gravity = 1000

var velocity := Vector2.ZERO

export (float, 0, 1.0) var friction = 0.1
export (float, 0, 1.0) var acceleration = 0.25

var last_collission = null;

func get_input():
    var dir = 0
    if Input.is_action_pressed("ui_right"):
        dir = 1 
        $RightRay.enabled = true
        $LeftRay.enabled = false
    if Input.is_action_pressed("ui_left"):
        dir = -1 
        $RightRay.enabled = false
        $LeftRay.enabled = true
    if dir != 0:
        velocity.x = lerp(velocity.x, dir * speed, acceleration)
    else:
        if !is_jumping:
            $RightRay.enabled = false
            $LeftRay.enabled = false
        velocity.x = lerp(velocity.x, 0, friction)

var is_jumping = false
func _physics_process(delta):
    get_input()
    velocity.y += gravity * delta
    var snap = transform.y * 128 if !is_jumping else Vector2.ZERO
    velocity = move_and_slide_with_snap(velocity.rotated(rotation),
                    snap, -transform.y, true, 4, PI/3)
#   velocity = velocity.linear_interpolate(velocity.rotated(-rotation), 0.8)
    velocity = velocity.rotated(-rotation)

    if is_on_floor():
#       velocity.x += speed * delta
        if !is_jumping :
            rotation = get_floor_normal().angle()  + PI/2

        is_jumping = false

        if Input.is_action_just_pressed("ui_accept"):
            is_jumping = true
            velocity.y = jump_speed
            $RightRay.enabled = true
            $LeftRay.enabled = true

    if  $RightRay.is_colliding():   
        rotation =  $RightRay.get_collision_normal().angle() + PI/2
        $RightRay.enabled = false
        $LeftRay.enabled = false

    if  $LeftRay.is_colliding():        
        rotation =  $LeftRay.get_collision_normal().angle() + PI/2
        $RightRay.enabled = false
        $LeftRay.enabled = false
Godot version 3.4
in Engine by (54 points)

just experiment with numerical values of this line :

var snap = transform.y * 128 if !is_jumping else Vector2.ZERO
velocity = move_and_slide_with_snap(velocity.rotated(rotation),
                    snap, -transform.y, true, 4, PI/3)

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 Frequently asked questions and 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 [email protected] with your username.