Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Razputin · Mar 12, 2021 at 07:21 PM · raycastscreentoworldpointworldtoscreenpoint

Function to convert UI Position to actual position?

This is hard to put into words so I hope the picture helps. My guns are in my UI, as they are 2D sprites. Each gun may have a different position for it's muzzle so I want to draw a ray using the muzzle of the sprite and the players transform.forward.

I need to convert the UI's position from where it is in the canvas to where it is on the screen.

Please let me know if that makes sense, or is even possible.

alt text

question.png (445.7 kB)
Comment
Add comment · Show 2
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 jackmw94 · Mar 13, 2021 at 12:10 AM 0
Share

I think that sometimes we imagine behaviours that don't exist. Your bottom image shows you hitting an object in the exact centre of the screen but if that enemy was a few meters back, still centred and you continued drawing your line it'd go off to the left. I think if you were to implement this behaviour it would feel very weird as the player would have to get the enemy in the top left part of the screen if they were far away. Since your gun is mostly aligned with the camera, I think that using the camera's forward ray as your bullet line will behave a lot more expectedly.

avatar image Razputin jackmw94 · Mar 13, 2021 at 02:01 AM 0
Share

Thanks I see what you're saying, I'm actually using the players forward for hit detection. This will be for drawing a line renderer from the guns muzzle to the hit.point to imitate bullet trails.

Unless you have a different suggestion on a way to do this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Mar 12, 2021 at 10:20 PM

What you are going to want to use is Camera. ScreenToWorldPoint(new Vector3 (muzzPos.x, muzzPos.y, distanceFromCam));. That will be your muzzle position translated to world space

Comment
Add comment · Show 2 · 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 highpockets · Mar 12, 2021 at 10:25 PM 0
Share

Your setup potentially requires this instead RectTransformUtility.WorldToScreenPoint(); while passing camera as null

avatar image Razputin highpockets · Mar 13, 2021 at 02:21 AM 0
Share

Thanks for the suggestions, these are what I've been trying to implement but I haven't been able to find the solution.

This is for casting a linerenderer from the muzzle to the raycasts hit.point. These are the implementations that I've tried.

         Vector3 v = RectTransformUtility.WorldToScreenPoint(null, fps.firePoint.transform.position);
         
         fps.lR.SetPosition(0, v);

         Vector3 v = Camera.main.ScreenToWorldPoint(new Vector3(fps.firePoint.position.x, fps.firePoint.position.y, maxFireDist));
         
         fps.lR.SetPosition(0, v);
 
         Vector3 v = Camera.main.ScreenToWorldPoint(fps.firePoint.transform.position);
 

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

162 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to track a world object in a Screen Space - Camera Canvas 1 Answer

Find TextureCoordinates from plane with out raycast only using mouse position on plan 0 Answers

How to achieve a more accurate Mouse to WorldCoordinates & faster updating of object follow? 2 Answers

Raycast, ScreenToWorldPoint accuracy problem 4 Answers

Camera.main.ScreenToWorldPoint not outputting expected results. 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