Problem with raycast

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

So, ummmm…

I have a problem with raycast. I’m creating a simple game and i want to attack enemy’s.
I create enemy and everything works fine but… If I duplicated him the raycast gone wrong.

The script cannot detect duplicated enemy’s raycast. If i change duplicated enemy raycast name and write it to script error show message: node not found. If i change original enemy’s raycast name and write to script it works fine on original enemy but on duplicated raycast didn’t want to detect collision, like raycast don’t exist.

Maybe different code? it’s the same with quotation marks

The game is 2D.

Photos and everything:
Duplicated enemy
Original enemy

I think that duplicating this will create some problems. From what I understand, if you try to duplicate that , it will share the same script. So the solution is to make the ‘enemy’ into a new scene. And then , you can just instance it over and over again.

To add effectiveness, in your root node script, add:

export(PackedScene) var Enemy 

And then add the enemy.tscn you’ve created earlier in the editor.
Once you have that , you can do whatever you want with it:

func insert_logic_here():
     var t = Enemy.instance()

Example code:

var no_of_enemy = 5
func _ready():
     for i in no_of_enemy:
         var t = Enemy.instance()
         t.global_position = get_node("spawner").global_position

Of course you can do more than this but it is up to you

Mrpaolosarino | 2021-05-08 03:56

I tried set enemy’s in scene in normal style, maybe it will work but in enemy script i have reference to player for this scene so it’s kinda bad, didn’t work. Do you think it will work using your way? I’m not really good at gd script, if you can help me export(Packed Scene) var Enemy must be in main node in scene (parent of all)? if I need any references for example like Enemy = get_node('enemy') or it must be in enemys node, global script etc. I added this script in main node and enemy’s won’t show up even i added normal enemy node. I put insert_logic function and rename to _physics_process(delta).

SatinOregano503 | 2021-05-08 09:35

If i turn insert_logic into physicsprocces(delta) i get error Invalid call. Nonexistent function ‘instance’ in base ‘Nil’. If insert_logic function added normally it works without errors but none of enemy’s will show up

SatinOregano503 | 2021-05-08 09:37

enter image description here

SatinOregano503 | 2021-05-08 09:38

Main node i mean node ‘World’

SatinOregano503 | 2021-05-08 09:42

I’ll base my answer in your latest screenshot above.
First, click your “enemy” node and save it as a scene.

Second, in your main node, go to your editor and you will see your export variable enemy (which is empty)

Third, drag your “enemy” tscn in that export variable enemy


The reason why it is becoming a null because you still need to add the enemy scene to instance it.

And , yes, this is on main node.

And the reason why insertlogic is working but enemy not showing is the “insertfunc” is not called that’s why.
This is why by the time you make it a physics_process, it creates a error which means that your code is working.

Mrpaolosarino | 2021-05-08 11:48

Also beware, since physics process calls every frame, your enemy is created every single frame which makes it laggier.

Add some limitations like instead of using physicsprocess , use ready so that this will call only once

Mrpaolosarino | 2021-05-08 11:51

It looks ok, but I have the player node signals connected, if the enemy is in the scene these signals are not connected. Do you know what to do in this situation? Is there a way to do this?

SatinOregano503 | 2021-05-08 12:21

The signal is area entered.

SatinOregano503 | 2021-05-08 12:24

I also have references to player nodes, there are a lot of conflicts due to the enemy being a scene

SatinOregano503 | 2021-05-08 12:29

if nothing can be done about it, maybe you can help me with taking the node of the enemy into memory and then I could spawn him like scene before if he died. Duplicating not working so maybe this will work if there’s a way.

SatinOregano503 | 2021-05-08 12:33

  1. you can use “connect”.
  2. Send me the script of your referenc
  3. The only way you can solve the duplicating not working is by making your enemy into a new scene, if not then you’ll have to find it for yourself. Saving it to memory will make it complicated

Mrpaolosarino | 2021-05-08 12:57

Code that connects to player:

func _on_SwordHit_area_entered(enemy):
target_1 = $"Raycast/RayCastLeft".is_colliding()
target_2 = $"Raycast/RayCastRight".is_colliding()
if target_1 || target_2 == true:
	health_bar.value -= 50

func _on_SwordHit_fast_area_entered(enemy):
target_1 = $Raycast/RayCastLeft.is_colliding()
target_2 = $Raycast/RayCastRight.is_colliding()
if target_1 || target_2 == true:
	health_bar.value -= 20

Only taking player node:

if laser.is_colliding():
		health.value -= 10

And taking nodes also:

onready var health = get_parent().get_parent().get_node("Player/HealthBarPlayer/HealthBar")

enter image description here

SatinOregano503 | 2021-05-08 13:07

I think that area entered code can be write in player but what with taking node to enemy’s healtbar and also taking damage to player’s healthbar.

SatinOregano503 | 2021-05-08 13:15

SORRY, laser is working so getting node is working but signal to func (area entered) are not connected. So writing script in player can solve it but what if on map are more than 1 enemy, i’m going to check it.

SatinOregano503 | 2021-05-08 13:30

So it’s a lot of bugs. Writing code in player didn’t solve 0 dmg problem. Enemy has problem with animations and finding sprite frames,couldn’t resolve track also.

enter image description here

SatinOregano503 | 2021-05-08 13:45

enter image description here

SatinOregano503 | 2021-05-08 13:47