Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Turtlemaster · Feb 24, 2014 at 02:14 AM · camerapositionrayviewportcrosshair

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

alt text

crosshair.jpg (282.6 kB)
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 24, 2014 at 02:37 AM 0
Share

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.

avatar image Turtlemaster · Feb 24, 2014 at 02:43 AM 0
Share

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

avatar image Turtlemaster · Feb 24, 2014 at 03:34 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Turtlemaster · Feb 24, 2014 at 04:25 AM 0
Share

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

avatar image robertbu · Feb 24, 2014 at 04:49 AM 0
Share

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.

avatar image Turtlemaster · Feb 24, 2014 at 04:51 AM 0
Share

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)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Second Camera Position on Viewport to UI Element Position on Screen 2 Answers

When switching camera position... 1 Answer

camera zoom in/out position based on object's speed 1 Answer

Move an object in world space on the viewport 0 Answers

RTS Camera movement wrong after rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges