sorry if this can be considered a low quality post, but i coded my mobile game with mouse controls because i didn’t know godot could emulate touch, so i’m trying to convert my mouse code to touch code but i can’t get it to work, this is the touch code:
extends RigidBody2D
@onready var line_2d: Line2D = $Line2D
var launch_multiplier = 5
var direction = 0
var length = 0
var launch_vector = 0
var startmosloc = 0
func _input(event: InputEvent) -> void:
var ballpos = global_position
line_2d.global_position = ballpos + Vector2(8, -50)
if event is InputEventScreenTouch:
startmosloc = event.position - global_position
elif event is InputEventScreenDrag:
line_2d.set_point_position(1, ((event.position - global_position) - startmosloc) * -1)
if event is InputEventScreenTouch and not event.pressed:
launch_vector = line_2d.to_global(line_2d.points[0]) - line_2d.to_global(line_2d.points[1])
length = launch_vector.length() * -1
direction = launch_vector.normalized()
linear_velocity += direction * length * launch_multiplier
if event is InputEventScreenTouch and event.is_pressed():
linear_velocity = Vector2(0, 1500)
and this is the mouse code:
extends RigidBody2D
@onready var line_2d: Line2D = $Line2D
var launch_multiplier = 5
var direction = 0
var length = 0
var launch_vector = 0
var startmosloc = 0
func _process(delta: float) → void:
var ballpos = global_position
if Input.is_action_just_pressed("swipe"):
startmosloc = get_global_mouse_position() - global_position
if Input.is_action_pressed("swipe"):
line_2d.global_position = ballpos + Vector2(8,-50)
line_2d.set_point_position(1,((get_global_mouse_position() - global_position) - startmosloc) * -1)
if Input.is_action_just_released("swipe"):
launch_vector = line_2d.to_global(line_2d.points[0]) - line_2d.to_global(line_2d.points[1])
length = launch_vector.length() * -1
direction = launch_vector.normalized()
linear_velocity += direction * length * launch_multiplier
if Input.is_action_just_pressed("smash"):
linear_velocity = Vector2(0, 1500)
The actions are simply left click for swile and right click for smash, i used mouse inputs as a temporary way of “emulating” touch because i didn’t know an official option was in project settings.
What i simply wanted to do was a slingshot like mechanic when you hold your finger and drag anywhere on the screen, and while the body was in midair, you could tap to stop it midair and make it go down, but since i’ve never developed with godot’s 2 touch screen inputs, i couldn’t get it to work
Are there any errors being thrown in the debugger while you’re playtesting? It looks like you don’t create the points on the Line2D before attempting to access them. Refer to the Line2D reference in the documentation, maybe than can point you in the right direction.
Also, you can avoid inverting the length by subtracting the second point from the first point, so that the launch_vector points in the desired direction without messing around with normalizing or vector magnitudes:
I think since your Line2D is a child of the RigidBody2D you’re trying to move, you’re getting a lot of unexpected behavior, such as the line2D inheriting the orientation of its parent. It might make it simpler if you separated the Line2D from the ball. Make the Line2D a sibling with the body instead of a child, for example.
Also, you’re having to constantly update the global position of the line when if they are separate, you can simply update the position of the first point. I threw together a simplified example that I believe would achieve what you want here:
extends RigidBody2D
@onready var line_2d: Line2D = # Get Line2D Here
var launch_multiplier = 5
var launch_vector = 0
var startmosloc = 0
func _process(delta: float) -> void:
if Input.is_action_just_pressed("click"):
startmosloc = get_global_mouse_position()
if Input.is_action_pressed("click"):
line_2d.set_point_position(0, global_position)
line_2d.set_point_position(1, global_position + get_global_mouse_position() - startmosloc)
if Input.is_action_just_released("click"):
launch_vector = line_2d.points[1] - line_2d.points[0]
linear_velocity += launch_vector * launch_multiplier