0 votes

I have a tank project going and have come across a problem with area collision for my turret aiming. what will happen is the turret aims at targets as they enter the area, if I drive two tanks into the area , then drive one tank out the enemy tank will lose the remaining tank even if it was the tank it was targeting. I have on body exited set to change target to null , but even if I change that to just pass the tanks just keep target lock after they exit the area leaving the enemy or player tank targeting an exited body while other bodies are still in the area.

can I check the area for remaining tanks? or maybe store targets as they enter the area would work?

in Engine by (83 points)

This is a very confusing explanation. It is much better and much simpler to just share your code. Please share your tank controller script as well as a screenshot of your scene tree.

Thank you Bernard Cloutier for the reply , sorry my question is confusing , the turret has an area 2d with a collision shape to detect a tank in range and target the intruding body , it was adapted from KidsCanCode tank tutorial. I just needed to expand on the idea so I added an array to store the intruding bodies and then remove them in the on body exited function and targeting is not set to null until the array is empty, it seems to work good , I used "array.pushback(body)" so the turret doesn't re-target for each unit that enters its field.

Please copy and paste your code and add screenshots of your project's scene tree. It is very hard to diagnose the source of the problem without them.

1 Answer

0 votes

Thank you Bernard Cloutier , I found a solution to my problem. I will pass on the image post but here is the solution I used.

var in_range = []

func _on_DetectRadius_body_entered(body):
  in_range.push_back(body)
  target = in_range[0]

func _on_DetectRadius_body_exited(body):
  var x = in_range.find(body)
  if x > -1:
      in_range.remove(x)
  if in_range.size() == 0:
      target = self
  else:
      target = in_range.pop_front()

pasting code here does not seem to like tabs so indents are off.

by (83 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.