0 votes

Related to: https://godotengine.org/qa/86271/how-can-i-make-the-player-move-by-swing

I made the swing mechanic myself but my problem now it's that i can't go backwards passing the viewport rect of 1920x1080 pixels:
https://imgur.com/a/V977eIb

This is my code:

extends RigidBody2D


const MAX_DISTANCE = 150
export(int) var elastic_factor = 3
var launch_force := Vector2()
onready var rest = $RestPosition

func _on_touch_pressed(_event: InputEventScreenTouch):
    pass

func _on_touch_drag(event: InputEventScreenDrag):
    mode = RigidBody2D.MODE_KINEMATIC
    launch_force = (rest.global_position - event.position).clamped(MAX_DISTANCE)

func _on_touch_released(_event: InputEventScreenTouch):
    mode = RigidBody2D.MODE_RIGID
    apply_central_impulse(launch_force * elastic_factor)
    launch_force = Vector2()

func _unhandled_input(event):
    if event is InputEventScreenTouch:
        if event.pressed:
            _on_touch_pressed(event)
            get_tree().set_input_as_handled()
        else:
            _on_touch_released(event)
            get_tree().set_input_as_handled()
    if event is InputEventScreenDrag:
        _on_touch_drag(event)
        get_tree().set_input_as_handled()
in Engine by (122 points)
edited by

1 Answer

0 votes
Best answer

Solved it:

func _on_touch_drag(event: InputEventScreenDrag):
    var event_global = get_canvas_transform().affine_inverse().xform(event.position)
    launch_force = (rest.global_position - event_global).clamped(MAX_DISTANCE)
by (122 points)
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.