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!