+1 vote

I am making a 3D space shooter and I am having trouble figuring out how to make arrow icons that point in the direction of an offscreen enemy (similar to how no man's sky does it).

The godot "waypoints" demo project is a good starting point but it doesn't take into account the z rotation that a ship would normally have. I haven't found any solutions after googling for a while and it's a hard concept for me to grasp.

I know that I need to unproject the enemy's translation but if the enemy is offscreen I'm not sure how to make it so the arrow points in the proper direction.

red arrow shown

Godot version 3.2.3
in Engine by (30 points)

1 Answer

+2 votes

camera.unproject_position(point) gives you the screen coordinate of a 3D point seen by the camera.
You can easily make an arrow that look at that point when this coordinates are offscreen.

var point=camera.unproject_position(ship.translation)
if point.x<0 or point.y<0 or point.x>get_viewport().size.x or point.y>get_viewport().size.y:

now you should define a positionate_arrow function to place it on the border of the screen, cant think about anything smart right now, but hopefully you got the point.
if placing the arrow on a circle around the center of the screen is enough for you (as it seems is enough for no man sky), you can go with

 func positionate_arrow(point):
  var circle_radius=min(get_viewport().size.x, get_viewport().size.y)/2
by (1,436 points)
edited by
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.