x


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 =)

more ▼

asked Dec 30 '11 at 08:43 PM

JPB18 gravatar image

JPB18
74 11 18 23

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Dec 30 '11 at 10:37 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Thank you very much for your answer.

It works but there is still one problem: The Crosshair fails to accompany the object, often taking much time to put itself in front of it.

My current code is this one:

//This script keeps the crosshair in front of the player

using UnityEngine; using System.Collections;

public class CrossHairforward : MonoBehaviour {

public Texture2D cursorImage;
public int cursorWidth = 32; //Width of the cursor texture
public int cursorHeight = 32; //Height of the cursor texture
public Transform myTransform;
public Camera myCamera;
public int cursorDistance = 1;



// Use this for initialization
void Start () {
myTransform = transform;    //So you don't GetComponent your transform with every OnGUI call

Screen.showCursor = false; }

// Update is called once per frame
void Update () {
Screen.showCursor = false;
}

void OnGUI() {
    Vector3 aimPos = myTransform.position + myTransform.forward * cursorDistance;
    Vector3 chPos = myCamera.WorldToScreenPoint(aimPos);
    chPos.y = Screen.height-chPos.y;
    GUI.DrawTexture(new Rect(chPos.x - cursorWidth/2, chPos.y - cursorHeight/2, cursorWidth, cursorHeight), cursorImage);
}

}

Dec 30 '11 at 10:52 PM JPB18

Oh wait, managed to make it work just like I wanted... Just had to parent the CrossHair object with the fighter :P

Thank you very much! :D

Dec 30 '11 at 11:25 PM JPB18
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4172
x1095
x5
x3

asked: Dec 30 '11 at 08:43 PM

Seen: 600 times

Last Updated: Dec 30 '11 at 11:25 PM