Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | System_Error |
Hi all. I have been working on a little game for a bit, and now would like to get something to work in a slightly different way.
extends RigidBody2D
const MAX_MINE_SPEED = 100
var detonation_scene = preload("res://Explosion.tscn")
var mine_speed = Vector2()
var targets = []
onready var timer = $ImpulseTimer
# If a target enters "DetectionArea"…
func _on_DetectionArea_body_entered(body):
if body.is_in_group("Player"):
print("Target detected!")
targets.append(body)
timer.start()
func _on_ImpulseTimer_timeout():
var mine_pos = self.global_position
var target_pos = targets.back().global_position
var pos_diff = target_pos - mine_pos
var n_pos_diff = pos_diff.normalized()
apply_impulse(target_pos, n_pos_diff * 3)
clamp(mine_speed, 0, MAX_MINE_SPEED)
# If a target leaves "DetectionArea"…
func _on_DetectionArea_body_exited(body):
if body.is_in_group("Player"):
print("Target lost!")
var targetindex = targets.find(body)
targets.remove(targetindex)
if targets.size() == 0:
timer.stop()
func _on_Mine_body_entered(body):
var stage_node = get_parent()
var mine_pos = self.global_position
var detonation_instance = detonation_scene.instance()
if body.is_in_group("Destroyable"):
detonation_instance.position = mine_pos
stage_node.add_child(detonation_instance)
body.queue_free()
self.queue_free()
I’d like to use Groups, and the PlayerShip is in two: “Destroyable” and “Player”. Mines are also in “Destroyable”, so they can explode when they collide with each other. Currently, though, they don’t move. Am I missing something?
What node uses that code? A player? A mine?
SIsilicon | 2018-12-05 18:35
This would be Mine.gd.
System_Error | 2018-12-05 21:01