How do I use Groups to have one kind of object follow the player?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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

:bust_in_silhouette: Reply From: iron_weasel

I don’t know if that timer is setup correctly based on the provided sample. I don’t know if the objects are triggering the sensors you have setup. Objects are going going to be detected if the collision masks and layers are setup up correctly. This is separate from groups.

However, I think I see problems with the function giving force to the mine. apply_impulse is relative to the RigidBody2D’s local space. Therefore, Vector2(0, 0) would be the center of the mine. Second, these are accelerating forces therefore the mine might need more force especially depending on the dampening or other forces acting on it.

I don’t think clamp does what you want it to do.

Therefore you should check the following:

  1. Make sure your mask and layer flags are setup for correctly for all nodes involved.
  2. Make sure your timer is setup correctly.
  3. Make sure your forces are being applied in the correct location (try <0, 0>) and are big enough to actually budge the thing.
  4. Clamp the speed with a different method, because what you have there is might not be any kind of correct.

Yeah… I’d removed clamp a while ago. I’ll look through, and see what I can do about that other stuff. Thanks!

System_Error | 2018-12-06 01:38