Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by karlkerschl · Nov 09, 2017 at 02:29 AM · mousepositionscreentoworldpointscreenpointtoraydrag objectsperspective camera

ScreenToWorldPoint offset affecting ScreenPointToRay ?

Hi, all. I recently changed a project from an orthographic camera (which was super easy and intuitive) to a perspective camera and I've run into some problems with accurate dragging in world space.
I've got one script in which I use ScreenPointToRay to follow the mouse around and get collision info from certain objects (which seems to work fine):

 void Update () {
             Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit rayInfo;
             bool didHit = Physics.Raycast (toMouse, out rayInfo, 500.0f);
             if(didHit) {
                 // Do some stuff
                 }
     }

And I have a different script which uses ScreenToWorldPoint to drag cards around the screen:

 void OnMouseDrag (){
             Vector3 temp = Input.mousePosition;
             temp.z = dragOffset; // I'm using an offset of 8f, which seems to give me a fairly accurate drag on screen
             Vector3 cardPosition = Camera.main.ScreenToWorldPoint (temp);
             transform.position = new Vector3 (cardPosition.x, cardPosition.y, -2f); //z position is adjusted so that the card passes in front of other objects rather than behind or through.
         }

My problem is that as soon as I use that drag script with the offset, my raycast is also offset and I no longer get accurate collision results.
I can't figure out why the drag offset in the ScreenToWorldPoint script would affect the ScreenPointToRay script. Should they not be used together? Even though they're in different scripts? Any pointer in the right direction would be helpful! Thanks in advance!

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
0

Answer by karlkerschl · Nov 09, 2017 at 05:33 PM

UPDATE: Alright, I think I've solved my own problem.
Instead of using ScreenToWorldPoint in the second dragging script I used another ScreenPointToRay and instead of doing it in the MouseDrag () function I did it in Update (). That seems to have given me much better dragging results. It looks like this now:

 public GameObject dragSurface;
 public Vector3 dragCoords;
 public LayerMask hitLayers;
 
 void Update () {
             Vector3 mouse = Input.mousePosition;
             Ray castPoint = Camera.main.ScreenPointToRay (mouse);
             RaycastHit hit;
  
             if(dragSurface.GetComponent<BoxCollider>().enabled) {
                  if (Physics.Raycast(castPoint, out hit, 500.0f, hitLayers)) {
                 dragCoords = new Vector3 (hit.point.x, hit.point.y, -2.0f);
                  }
             }
         }

MouseDrag () now just updates the position of the card using dragCoords.
I still needed the card to be dragged in front of other objects so I created a collider called dragSurface closer to the camera to cast the ray into. This works great to reduce the dragging FOV offset but it caused problems by blocking all of my other MouseDown collisions. No masking or physics layer matrix trickery would ignore it so I hacked together a fix that basically just turns its collider on when I need to drag something and then turns it off again.
Kind of ugly, but oh well.
Maybe a plane or something would work better than a box collider? I dunno. I'd love a more elegant solution.

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

118 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

Related Questions

Set position flom RaycastHit.point 2 Answers

why is my mouse posision allways 0? 1 Answer

3rd Person Shooter Crosshair system 1 Answer

Screen to World Point on 2.5D game 0 Answers

Mouse position is offset from where it should be based on actual mouse position from origin. 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