+1 vote

Hello guys as you see scroll container doesnt have inertia property.
How we can add this functionality to the scroll to smooth scroll effect?

in Engine by (17 points)

1 Answer

+1 vote

This can be done by creating your own scroll-container; create a Control for clipping, under that a Control that will act as the origin and will move around based on input.

So the scene tree would be:
MyCustomScrollBox → origin → bigstuffthatneedsscrollin

With the MyCustomScrollBox script being:

extends Control

var v = Vector2(0,0) #current velocity
var just_stop_under = 1
var multi = -4 #speed of one input
var is_grabbed = false

func _process(delta):
    v *= 0.9
    if v.length() <= just_stop_under: v = Vector2(0,0)
    $origin.rect_position += v

func _gui_input(event):

    if event is InputEventMouseButton:
        match event.button_index:
            BUTTON_MIDDLE:  is_grabbed = event.pressed

    if event is InputEventMouseButton:
        match event.button_index:
            BUTTON_WHEEL_DOWN:  v.y += multi
            BUTTON_WHEEL_UP:    v.y -= multi
            BUTTON_WHEEL_RIGHT: v.x += multi
            BUTTON_WHEEL_LEFT:  v.x -= multi
by (891 points)

Thanks man, I will try it.

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.