- Home /
Crosshair Not Parallel To camera
I want to create a crosshair that is not centered (60% down from the top). This seems to create a horrible new problem for me, I'm having trouble lining up my bullets with the crosshair. (I can implement the primitive solution of rotating my bulletSpawn until it looks right but I'd prefer to figure out a more technical approach. The green line is a ray going through the crosshair's position in the viewport. The yellow line is a line that shoots from the crosshair outward parallel to the camera. As you can see, neither of them line up perfectly with where the crosshair is pointing, interestingly it looks as if the crosshair is perfectly in the center of those 2. Does anyone have any knowledge of what is going on? =(
//Setting crosshair position:
position = new Rect( ( Screen.width - crosshairTexture.width ) *.5f, ( Screen.height - crosshairTexture.height ) *.6f,
crosshairTexture.width, crosshairTexture.height );
//Draw ray from camera through crosshair position:
Ray ray = Camera.main.ViewportPointToRay(new Vector3(.5f, .4f, 0));
Debug.DrawRay(ray.origin, ray.direction * 10, Color.green);
You didn't provide the Debug.DrawRay() code for the other rays, so I'm not sure what I'm looking at. If the projectiles come out of the gun, then you will need to triangulate. That is, you will either need to picked a fixed distance from the gun as the point you are ai$$anonymous$$g at, or you will need to do a Raycast() and use the hit.point as the specific target for the gun. Either why, you need to find the point in 3D space if you want the gun to aim correctly.
@Robertbu: The only ray that matters for my question is the green one. I need to figure out a vector based on which to orient my bulletSpawn point. The bullets are going to be spawned from the center of the camera, as if co$$anonymous$$g out of the guy's face.
I made a halfway vector between the green ray and the camera's forward, which results in more accuracy the closer I am to the target (still not viable solution, but its closer than I was before.)
I want the shot to come Out of the crosshair. Not out of the gun. I just can't create a ray that goes out of the camera into the world in the same spot that the crosshair actually looks like its pointing at when it is drawn on the screen.
Answer by robertbu · Feb 24, 2014 at 04:00 AM
I am assuming that 'position' is the GUI coordinates of the crosshair. That is you are drawing the crosshair with GUI.DrawTexture(). If so, and if I did my math right, this is the calculation for the ray:
float x = Screen.width / 2.0;
float y = 0.4 * Screen.height + 0.1 * crosshairTexture.height;
Ray ray = Camera.main.ScreenPointToRay(new Vector3(x, y, 0));
Here is an alternate calculation:
float x = Screen.width / 2.0;
float y = Screen.height - (position.y + crosshairTexture.height/ 2.0)
Ray ray = Camera.main.SCreenPointToRay(new Vector3(x, y, 0));
Note these two different calculations do two things. First they find the midpoint of the texture in GUI coordinates. Then they convert the GUI coordinates to Screen coordinates.
Wow, thanks a lot, that worked! (I had to get rid of the extra 0.4) I still dont understand how it works though... Why does .1 crosshairTexture.width; do anything? and on the second calculation, isnt that the exact same thing as ViewportPointToRay(new Vector3(.5f, .4f, 0)); if my texture is centered at 40% up from the bottom of the screen..
I don't think your calcualtion puts your crosshair where you think it does. That is probably the root of your whole issue. If you really wanted your crosshair centered at 40% up from the bottom, the calculation for the GUI coordinate would be:
Screen.height - 0.5 * crosshairTexture.height
Your calculation works out to:
0.6 * Screen.height - 0.6 * crosshairTexture.height
Note my use of crosshairTexture.width should have been crosshairTexture.height. I've fixed my answer.
Ahh... I see. Well I feel like an idiot. Again thanks a lot. (and yea the texture was square so width wasn't a problem)