![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Tomi |
Hello!
In my program there are two moving objects: player’s bullets and the monsters.
This is the script that move the monsters:
extends KinematicBody2D
var team="enemies"
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func _process(delta):
position.x+=4
if position.x>(get_viewport_rect().size).x:
queue_free()
and the script of the bullets:
extends KinematicBody2D
var itsspeed = 4
var team="bullets"
func _physics_process(delta):
var collenemy=move_and_collide(Vector2(itsspeed,0).rotated(rotation))
if position.x<1 or position.y<1 or position.x>(get_viewport_rect().size).x or position.y>(get_viewport_rect().size).y:
queue_free()
elif (collenemy):
if collenemy.collider.team=="enemies":
collenemy.get_collider().queue_free()
var pk=get_node("/root/Node2D/level1/pointlabel")
pk.point+=1
pk.text="Score: "+str(pk.point)
queue_free()
The first problem when I coded this that when more than one monster are in the screen, the bullets shot down the first enemy but the others push away the bullets.
Besides I couldn’t get the properly name of collider with this code:
if collenemy.collider.name=="monster":
Therefore I was forced to use an own variable (“team”) to identity the collided objects. It works, but I don’t know why push away the enemies the bullets and the bullets each other otherwise?
The second problem is that as if would exist an invisible instance of the enemy monsters on the screen, although I load it from another scene. But when I shoot, the bullets often destroy at the place where the monster exists, but in another scene.
And the Score indicates hit in this case.
Has anyone encountered a similar problem in own games and can help me?