Where is the object pointing?

Hello there!

So I need to know if there is any function that allows me to know in which direction my object is pointing. My intention in knowing this is to know where:

  1. To place the Crosshair (in a 3rd Person view where the crosshair is ahead of the player object);
  2. To know in which direction the gun will be fired;

Also please remember that this works in a 3D environment with no gravity and no “floor” (space), and C# code would be pleasantly preferable.

Thank you very much for the answers =)

You could use the object’s transform.forward vector: it will always point in the local +Z direction:

    // get the point "distance" units ahead the weapon:
    Vector3 aimPos = transform.position + transform.forward * distance;
    // convert it to screen coordinates:
    Vector3 chPos = Camera.main.WorldToScreenPoint(aimPos);

The screen coordinates are returned in chPos.x and chPos.y. If you’re going to use GUI.DrawTexture, remember that the GUI y axis runs upside down, thus you should use Screen.height-chPos.y to get the correct GUI y coordinate (chPos.x is ok).

NOTE: If your object is rotated from the conventional orientation (what often happens with imported models), maybe you must use transform.right (if X is the forward direction) or transform.up (if it’s Y).