CollisionShape2D is not working

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bikereczek

Hi there.
I’m beginning Godot’s programmer. I’ve problem with collision in my Tower Defense game.

WarScreen.tscn
WarScreen = Control
----Path2D = Path2D

Script Path2D.gd:

extends Path2D

onready var EnemyScene = preload("res://War/PathFollow.tscn")
var StartFirstEnemy = false
var Enemy = null

func _process(delta):
	if StartFirstEnemy == true:
		for i in Enemy:
			i.set_offset(i.get_offset() + 70 * delta)
	if Global.ClickStart > 0:
		var x = EnemyScene.instance()
		self.add_child(x)
		StartFirstEnemy = true
		Enemy = get_children()

PathFollow.tscn
PathFollowEnemy = PathFollow2D

Script PathFollow2D.gd:

extends PathFollow2D

func _ready():
	var enemy
	
	if Global.WhatOpponents == 0:
		enemy = preload("res://EnemyUnits/Goblin.tscn")
	if Global.WhatOpponents == 1:
		enemy = preload("res://EnemyUnits/Orc.tscn")
	
	var x = enemy.instance()
	self.add_child(x)

Goblin.tscn
Area2D = Area2D
----GoblinSprite = Sprite
----collision_shape = CollisionShape2D

Orc.tscn
Area2D = Area2D
----OrcSprite = Sprite
----collision_shape = CollisionShape2D

Tower.tscn
Tower = Node2D
----detection_area = Area2D
--------collision_shape = CollisionShape2D
----attack_speed = Timer

Script Tower.gd:

extends Node2D

onready var mob = preload() # <-???
var targets_within_range = []

signal shoot_projectile

func _on_detection_area_area_entered(area):
# Everything is good to this point.
	if "mob" in area.name:
		targets_within_range.append(area)	

func _on_detection_area_area_exited(area):
	if "mob" in area.name and targets_within_range.size() > 0:
		targets_within_range.erase(area)
    
func _on_attack_speed_timeout():
	if targets_within_range.size() > 0:
		var projectile_origin_pos = position + Vector2(32, 32)
		emit_signal("shoot_projectile", projectile_origin_pos, targets_within_range[0].position)

In script ,Tower.gd" it tracks down collision, but it dosen’ t track down what is it? - Orc or Goblin?

It dosen’ work:.

if "mob" in area.name:

I try substituted other things instead ‘‘mob’’, but it also doesn’t work.
This problem has harrowed me for week.
Please help me.
Thank you in advance.

:bust_in_silhouette: Reply From: Moldor2

The test statement should be something like this…
for(Areas) in area: if area.name == "mob": #DO SOMETHING

Hi.
I know this, but what do i have to load as ,mob’'?
Thanks.

bikereczek | 2020-09-25 15:15

Try adding orcs to a group called orcs and add goblins to a group called goblins and then test if the area is in group orcs or if it is in group goblins

Moldor2 | 2020-09-29 12:29

Hi.
Thanks, it works. I also know what I had to do in order to “if mob in area.name:” works.
But I have a new problem - in script “tower.gd” positions “Orc” and ''Goblin" are (0,0).
In my opinion everything is good.
But I need positions from script “PathFollow2D.gd” (var i.set_offset).
I need this to tower’s shot func (_on_attack_speed_timeout() - var target_within_range[0].position).
How to do this?

bikereczek | 2020-09-30 22:29

Sorry. Script “Path2D” (not PathFollow2D.gd).

bikereczek | 2020-09-30 22:35