Godot Version
v4.5.1
Question
I’m trying to make a game in Godot mobile. There a line should spawn between 2 Fingers touching the screen. This is already working, but if the line is colliding with something, it should be able to delete this object, or the object should delete itself… The problem there’s that it´s only working, if you enter the object with the line really fast. → If you leave the line on the Object, its not deleting itself.
So I already got the Line (CharacterBody2d) with two Childs (CollisionShape2d and Line2d). The code of the Line is the first bit of the Code:
The other object is a Area2d with two Childs (collisionShape2d and ColorRect). the code is the second one in the following code.
I´ve them put both of them in a main scene, which is just a Control Node above these two scenes.
I really hope, someone Is able to find a solution…
extends Node2D
@onready var collision_shape: CollisionShape2D = $CollisionShape2D
@onready var line_2d: Line2D = $Line2d
var rect := RectangleShape2D.new()
var finger_positionen = {}
var breite = 50.0
func _ready():
line_2d.visible = false
collision_shape.disabled = true
func _input(event):
if event is InputEventScreenTouch:
if event.pressed:
finger_positionen[event.index] = event.position
else:
finger_positionen.erase(event.index)
update_line()
elif event is InputEventScreenDrag:
finger_positionen[event.index] = event.position
update_line()
func update_line():
if finger_positionen.size() == 2:
line_2d.visible = true
collision_shape.disabled = false
var keys = finger_positionen.keys()
keys.sort()
var a = finger_positionen[keys[0]]
var b = finger_positionen[keys[1]]
line_2d.points = PackedVector2Array([a,b])
var length = (b-a).length()
rect.size = Vector2(length, breite)
collision_shape.shape = rect
collision_shape.position = (a+b)/2
collision_shape.rotation = (b-a).angle()
extends Area2D
func _physics_process(_delta):
await get_tree().physics_frame
var bodies = get_overlapping_bodies()
for body in bodies:
if body.is_in_group("line"):
queue_free()