WorldToScreenPoint for HUD targeting system problem

I’m sure I’m doing something obviously wrong here, but google is throwing me a lot of curve balls when I search with other problems that don’t seem to be directly related to what I’m trying to do here.

In essence I’m trying to create something akin to a targeting reticle on a FPS vehicle based game. The idea is that when you enable the reticle it appears on the HUD, starts to “lock” onto the bad guys position by moving towards his position at which point other actions will be available to the player.

This is my first foray into the HUD element so I’m learning a lot here and I’m aware that the co-ordinate space of the GUI calls is inherently different but I thought WordToScreenPoint was supposed to solve a lot of these.

At the moment I’m just testing drawing the reticle over the bad guy when I enable that element in the inspector. Here’s what I have:

function OnGUI() : void {
    if(reticleActive) {
    	//Get the bad guy position...
    	var badGuyPos : Vector3 = ourManager.getBadguyPos();
    	var reticlePos : Vector3 = ourCamera.WorldToScreenPoint(badGuyPos);
    	Debug.Log("Calculated Pos = " + reticlePos);
    	GUI.DrawTexture(Rect((reticlePos.x),(reticlePos.y),128,128),reticleTexture);
    }
}

This works… sort of. It draws the reticle in line on the screen x axis with the bad guy (maybe slightly offset) but on the y position it’s way below. If the bad guy moves left or right the reticle appears to track, but if the bad guy goes up the reticle goes down, so it’s almost like something is being inverted or needs to be inverted…

Again I feel like i’m just missing something obvious and am about to head to the documentation but wanted to see if anyone had any suggestions on areas for me to re-visit that would tell me what’s going on, or can blatantly see what I’m doing wrong I’d appreciate the info :slight_smile:

Edit: I modified the code to offset the position of the reticle by half of the width/height of the size of the reticle (and removed the magic numbers) which has corrected the x axis element where it was sort of offset, but the y axis portion continues to track inverted.

Thanks,
Greg

Ok I just figured it out… I recall reading something about one of the co-ordinate systems starting with y at the top of the screen not the bottom, and continued digging to realize that it was the GUI stuff.
The following change did it:

GUI.DrawTexture(Rect((reticlePos.x - reticleOffset),(Screen.height-(reticlePos.y+reticleOffset)),reticleSize,reticleSize),reticleTexture);

I don’t know if this is the most elegant solution … but it works for now.

indeed, I was just about to ask if this could go towards solving the problem:

http://forum.unity3d.com/threads/224258-Crosshair-depending-where-the-gun-is-aiming-(not-middlescreen)