Cannot set visibility based on distance

Godot Version

v4.3.stable.official

Question

I’m trying to make a system where I can target something based on my distance from it. Basically every targetable component, which includes the player has a sub-node of a billboarded sprite 3d that I turn visible whenever the aiming button is pressed to show that I’m targetting something.

The problem is, it only works if I’m really close or at a certain angle away from the object.

I’ve run the function with a print log next to it so that it will print something every time it should be turning the target visible and it seems to print every time I want it to be visible, but it doesn’t actually turn visible in the game.

Does anyone know what I might be doing wrong?

I tried just having the script detect if the currentTarget var was null, and if not, setting the reticle to visible, which made it target a bit more often than it was before but not whenever I pressed the aim button

Here’s the code I’m using for reference:

func _physics_process(delta):
	if is_multiplayer_authority() :
		if Input.is_action_pressed("Aim"):
			aim()
		if Input.is_action_just_released("Aim") :
			if(currentTarget != null) :
				currentTarget.targetReticle.hide()
			currentTarget = null
@export var targetRange:float
var currentTarget: RaceEntity = null
var targetIndex = 0
func aim() :
	#check all targets
	var targets: Array[RaceEntity]
	
	var closestTarget: RaceEntity = null
	var closestDistance: float = INF
	for potentialTarget in RaceEntity.allTargets :
		if potentialTarget == self || potentialTarget == subCharacter :
			continue
		
		var dist = position.distance_to(potentialTarget.position)
		if dist <= targetRange :
			targets.append(potentialTarget)
			if(dist < closestDistance) :
				closestDistance = dist
				closestTarget = potentialTarget
	
	if(currentTarget == null) :
		targetIndex = targets.find(currentTarget, 0)
		currentTarget = closestTarget
	
	#here allow for switching index
	
	if(currentTarget != null) :
		#print(currentTarget)
		currentTarget.targetReticle.show()

And here are some pictures of the game running for reference


Here I am pressing aim and nothing is happening


While here, only when I’m closer to this side does it seem to trigger consistently. I was wondering if maybe the geometry was somehow in the way, or it was calculating distance based on an offsetted position or something?

But the game will consistently print anything I tell it to in the same if statements where I ask it to turn the target visible whenever I press the button. It’d be working perfectly if I didn’t need it to actually turn the target visible, so I’m not sure exactly what’s going on. Any help is appreciated! Thank you for reading!

One thing that jumps out immediately to me is that your distance calculations are using position when IMO they should be using global_position. Position is a node’s position relative to its parent, so your code will only do what you’re intending if the player and all enemies have the same parent. Global position is a node’s position relative to an absolute origin. Most nodes in my games, for example, have position exactly 0 or near 0, but global positions can be arbitrary depending on where they are in the game world.

1 Like

Okay let me try switching it to that!

That would explain why it seems to be working at an offsetted position… Doesn’t explain why it’s printing at the position I thought it would but hey, let’s see.

Thank you!

Edit: Works perfectly now! Thank you so much! Funny how such a simple thing can cause a massive headache

1 Like