How do I fling the player

4.3
Hi , I’m working on a game ware you fling the player around , I’d followed this video on how to do it but when the video was finished, the code wasn’t working for me , please help
the code:
"

const SPEED = 100.0
const JUMP_VELOCITY = -200.0
const click_threshold := 9990
var is_dragging : bool = false
const flick_power = 2000

func _input(event: InputEvent) → void:
if event is InputEventMouseButton:
if event.is_pressed() and (event.global_position - global_position).length() < click_threshold:
is_dragging = true
elif is_dragging:
is_dragging = false
if (event.global_position - global_position).length() > click_threshold:
var flick_vector: Vector2 = (event.global_position - global_position)
velocity = flick_vector.normalized() * flick_power

func _physics_process(delta: float) → void:
velocity.x = SPEED
if not is_on_floor():
velocity += get_gravity() * delta
if is_dragging:
pass

move_and_slide()

"

You could probably have something under physics process like:

If on floor:
Velocity += Vector2.Zero
Else:
Velocity += Vector2.Zero
Global_position.y -= gravity

You are always setting the velocity.x to 100, which will move right if nothing collides. I assume removing this line will help immensely.


Make sure to format your code pastes

Make it easier for new users to format and preview code in their posts - #4 by gertkeno

i tried it but seems to not work

I kinda need it , in the game I’m making the player constantly move right like the game jetpack joyride , but I tried it but it dint work

What does “not work” mean though, what did you expect to happen versus what really happened? How did the code change exactly? How did your results change?

in the video I’ve watch, the guy can click on the player , drag to where he wants to fling to , and release the click to fling , however mine don’t move when i try to do it even if its moving or not , I think the video I’d watched was out dated because I’d tried deleted my code and copy his code and putting it in my player , but there was no result in that

Don’t hesitate to use triple back-quotes for code-blocks, with “gdscript” next to the first triple back-quotes

# Like this
const SPEED = 100.0
const JUMP_VELOCITY = -200.0

func _ready():
   print("hello")