Crosshair

I have been trying to make a cross hair for my game and I have been looking in the forums but I’ve just found how to make a crosshair for a normal fps, I want the crosshair move according to the position where a tank is shooting, not the camera.

can you please point me in the right direction.

Imma make a video in case you need more info to understand my problem.

thanks.

Have you tried the crosshair script in the FPS tutorial? http://forum.unity3d.com//viewtopic.php?t=26479&highlight=crosshair

If you are looking at using a GUI based on the position of an object perhaps something like Camera.WorldToScreenPoint would be worth checking out.

thx for the reply.

I have already checked the fps tutorial, like I said I need the crosshair to move, not the camera…

I’ll check the Camera.WorldToScreenPoint out and see what I can do with it.

thx again and more help would be appreciated.

If you explain your desired implementation in more detail, you might get some aditional ideas.

This is really dependent on what you want the crosshair to follow. The post above talks about following the mouse. If, however, your tank can only shoot the direction the tank or turret is facing, another implementation would be to determine the forward vector from the tank or turret (have a look at Vector3, the Vector3.forward property, etc) and then position the crosshair at a range along that vector. Different solutions to different designs…

Thx for your help.

here’s a video of the tank, I still need to make the crosshair.

I’ll post the video of the tank with the crosshair later, I hope.

Hi, I’ve the same problem… for example…Star Trooper, fire missile - How to create crosshair which is following vector for fired missile. I’ve absolutely no idea how to do that or where to implement.

i have the same problem. i have been trying to program a crosshair that follows the mouse because my game makes sure that we click the object to shoot it so i stuck my own script on the enemy so i need a mouse-following crosshair.

Im not too sure on how to exactly implement it in Unity, but my best guess would be having the crosshair as a GUITexture
You would need the position of your mouse in terms of pixels (since GUI in unity works on a pixel basis)
Then all that would be left to do would be to center your GUITexture on the mouse’s position each frame, which can be done in the Update() function as long as you have a reference to your GUITexture.

Hope it helps :slight_smile:

If you need to position the crosshair with the mouse, then you should check out this thread which shows how to send out a raycast from the camera at the mouse’s position. If the tank’s turret is controlled by direct rotation and elevation then you should firstly use a raycast in the direction of the gun to see what it is pointing at. Then, you can use Camera.WorldToScreenPoint to convert the hit point of the raycast to a position on the screen:-

var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);

if (Physics.Raycast(ray, hit)) {
  var crosshairScreenPos = Camera.WorldToScreenPoint(hit.point);
}

You can then use the GUI system to position the crosshair texture at the position you have calculated. You should subtract half of the crosshair’s pixel width and height from the screen position so that the centre of the texture is over the target point.