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:

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):

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:
    if event is InputEventScreenDrag:
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 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.