I have a crosshair image that will lock onto enemies that are visible in the camera and will stay on the edge of the screen when the enemy is offscreen. This works until you face more than 90º away from the enemy in either direction. If facing exactly 180º from the enemy, the indicator will appear as thought the enemy were directly in front of the camera but I would like it to stick to the closest edge of the screen instead. The red crosshair is drawn onto a 2d space and positioned within the camera view.
var enemy_pos:Vector3 = enemy.global_transform.origin var screen_pos:Vector2 = camera.unproject_position(enemy_pos) var lock:TextureRect = $TargetLock var lock_size:Vector2 = lock.texture.get_size() var crosshair_center:Vector2 = crosshair.rect_position + crosshair.rect_size * 0.5 var direction_to_enemy := (enemy_pos - camera.global_transform.origin).normalized() var direction_facing := -camera.global_transform.basis.z # currently unused, > 0 = target within 90º rotation # var facing_target = direction_facing.dot(direction_to_enemy) var screen_width = ProjectSettings.get_setting("display/window/size/width") var screen_height = ProjectSettings.get_setting("display/window/size/height") screen_pos -= lock_size / 2 screen_pos.x = clamp(screen_pos.x, 0, screen_width - lock_size.x) screen_pos.y = clamp(screen_pos.y, 0, screen_height - lock_size.y) lock.set_position(screen_pos)
I can understand the issue, but my vector math in 3d space is not good enough to solve this issue.