Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 Reverend-Speed · Dec 30, 2018 at 04:02 PM · mousepositionorthographic cameraraycaststexturecoord

Why does my render texture cursor appear offset from my mouse cursor?

Currently trying to get a scene working where I move the mouse pointer, which then moves a cursor sprite in worldspace, which is captured by a camera, which then displays the sprite and any other objects on a render texture. Project resolution size is based on Samsung TabA 2016 - 1280x800.

Unfortunately, I'm getting an odd offset of the cursor sprite from the mouse position and it's confusing the heck out of me.

When the mouse position is on the left side of the screen, the cursor sprite appears about 25% into the screen from the left hand side, and moving the mouse towards the bottom of the screen makes the cursor sprite vanish off-screen.

Could anybody help me figure out why I'm getting this offset?

[Here is a package][1] with the relevant scene, code and assets.

Thanks in advance. [1]: https://www.dropbox.com/s/scrqiuq2i5ekeog/remoteCursorPlacementRS.unitypackage?dl=0

Comment
Add comment
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

1 Reply

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

Answer by Reverend-Speed · Dec 31, 2018 at 08:08 PM

As it turns out, the solution was to get off my ass, tear out the old code (which only relied on camera.orthographicSize), do a little basic math and... voila... fixed HitTestUVPosition code:

 bool HitTestUVPosition(ref Vector3 uvWorldPosition)
     {
         RaycastHit  hit;
         Vector3     cursorPos   = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
         Ray         cursorRay   = sceneCamera.ScreenPointToRay(cursorPos);
 
         if (Physics.Raycast(cursorRay, out hit, 200))
         {
             Debug.DrawRay(cursorRay.origin, cursorRay.direction, Color.green);
             Debug.DrawRay(brushCursor.transform.position, cursorRay.direction, Color.yellow);
 
             MeshCollider meshCollider = hit.collider as MeshCollider;
 
             if (meshCollider == null || meshCollider.sharedMesh == null) return false;
 
             Vector2 pixelUV = new Vector2(hit.textureCoord.x, hit.textureCoord.y);
             float screenAspect = (float)Screen.width / (float)Screen.height;
             float camHalfHeight = canvasCamera.orthographicSize;
             float camHalfWidth = screenAspect * camHalfHeight;
             uvWorldPosition.x = (pixelUV.x * camHalfWidth * 2) - camHalfWidth;
             uvWorldPosition.y = (pixelUV.y * camHalfHeight * 2) - camHalfHeight;
             uvWorldPosition.z = 1.0f;
             return true;
         }
         else
         {
             Debug.DrawRay(cursorRay.origin, cursorRay.direction, Color.red);
             return false;
         }
     }

Easy when you eventually figure out how.

Comment
Add comment · 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

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

97 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

Related Questions

Problem firing projectile at mouse location 1 Answer

Cannot get mouse position in 2D using perspective 1 Answer

Unity 4.3 Skin mesh UV error at some frame after app back from background on ios 0 Answers

Use RayCasting and simulate collision physics 1 Answer

Raycast Inefficiency 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