Saturday, July 30, 2022
HomeGame Development2nd - Remove bouncing when switching/sticking to a unique floor

2nd – Remove bouncing when switching/sticking to a unique floor


I’m following this tutorial to make a KinematicBody2D align to the surfaces beneath it.

When switching from one floor to a different, the character bounces till it corrects/aligns its rotation, as proven right here:

animation showing character bouncing

How can I remove the bouncing?

I’ve uploaded a minimal model of the venture on GitHub.

The venture setup seems to be like this:
Godot editor screenshot

And that is the participant script:

extends KinematicBody2D

export (int) var pace = 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 Enter.is_action_pressed("ui_right"):
        dir = 1 
        $RightRay.enabled = true
        $LeftRay.enabled = false
    if Enter.is_action_pressed("ui_left"):
        dir = -1 
        $RightRay.enabled = false
        $LeftRay.enabled = true
    if dir != 0:
        velocity.x = lerp(velocity.x, dir * pace, 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 = remodel.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 += pace * delta
        if !is_jumping :
            rotation = get_floor_normal().angle()  + PI/2
        
        is_jumping = false

        if Enter.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
    

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments