Crosshair

Discussion in 'UnityGUI' started by sn4k3, Aug 14, 2009.

  1. sn4k3

    sn4k3

    New Member

    Joined:
    May 16, 2009
    Messages:
    36
    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.
  2. mayekol

    mayekol

    New Member

    Joined:
    Oct 23, 2007
    Messages:
    115
  3. sn4k3

    sn4k3

    New Member

    Joined:
    May 16, 2009
    Messages:
    36
    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.
  4. cosmicmesh

    cosmicmesh

    New Member

    Joined:
    Mar 19, 2009
    Messages:
    61
    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...
  5. sn4k3

    sn4k3

    New Member

    Joined:
    May 16, 2009
    Messages:
    36
  6. FIllbiker

    FIllbiker

    New Member

    Joined:
    Jun 9, 2010
    Messages:
    107
    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.
  7. mutantsproducer

    mutantsproducer

    New Member

    Joined:
    Jan 27, 2011
    Messages:
    6
    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.
  8. LostBalloon

    LostBalloon

    New Member

    Joined:
    Feb 12, 2011
    Messages:
    25
    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 :)
  9. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    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:-
    Code (csharp):
    1.  
    2. if (Physics.Raycast(ray, hit)) {
    3.   var crosshairScreenPos = Camera.WorldToScreenPoint(hit.point);
    4. }
    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.