问题描述
我想检查这样的代码:
func _input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
spawnTower(event.position)
如果当前鼠标位置有东西,例如。敌人、其他塔、地形等,并且只有当兴趣点没有碰撞时,才会生成一个塔。 我确实尝试从相反的站点解决这个问题:当鼠标进入/离开碰撞形状时,让每个碰撞形状信号。 想要添加最多 20 个塔和潜在的 100 个敌人,这似乎相当浪费。 对更好方法的提示将不胜感激。 亲切的问候,扬·马丁
解决方法
好吧,对不起,也许这对其他人或至少对未来的我有帮助。 所以这是我的解决方案: 首先:添加一个 RayCast2D,不需要脚本中的代码,确保在 Inspector 中选中“Collide With: Areas”。 第二:向主脚本添加一些代码,如:
func checkForCollision(position: Vector2):
get_node("RayCast2D").position = position
get_node("RayCast2D").cast_to = Vector2(0,0) # sets the length of the ray to 0
get_node("RayCast2D").force_raycast_update()
print($RayCast2D.is_colliding())
print($RayCast2D.get_collider ( ))
return $RayCast2D.is_colliding()
这将返回一个布尔值并打印一些附加信息。 在“调试”下拉列表中验证激活“可见碰撞形状”。 问候马丁