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
1
Question by adriandevera · Jun 29, 2015 at 03:31 PM · objectmousegridsnaprounding-errors

Accurate Mouse Snap to Grid within Threshold? (Float Rounding Errors)

Im having the same issue as this guy but the convo ended short with no solutions. (http://forum.unity3d.com/threads/snap-to-grid-and-mouse-position.155132/).

I know I need to add a threshold so my mouse can stay within a grid scale of 1 but not sure how to programmably approach it.

  • could just fix this issue by making the mouse hidden, but Id really like to see how this can be done in a proper way.* I really like the challenge but Ive spent about a day just thinking this through and Im baffled.

I dont know how to explain it further but maybe this image will help. Basically I want the mouse to hover over the entire (1 unit scale) grid. Im aware the behavior is due to rounding of Floats to Ints and that I would need a threshold to "clamp" the mouse to a 1x1 scale grid.

         Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition) ;
         Vector3 mousevector3 = new Vector3 (mouseray.origin.x, mouseray.origin.y, mouseray.origin.z);
 
         //apply grid to mouse hover
         if (Physics.Raycast (mouseray.origin, mouseray.direction, out projectorOrigin, 100)) {
             //snap to grid 1x1
             Vector3 gridpos = new Vector3(Mathf.Floor (projectorOrigin.point.x), Mathf.Floor (projectorOrigin.point.y), Mathf.Floor (projectorOrigin.point.z));
             grid.transform.position = new Vector3 (gridpos.x, gridpos.y, gridpos.z);
         }

Demonstration:

alt text

***Notice how my mouse only moves within the UpperRight of the main box of the actual grid.

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
6
Best Answer

Answer by adriandevera · Jun 29, 2015 at 03:52 PM

Solved it.

Rather than Mathf.Round use a function such as this. Make sure to add the variable snapValue!

     private float snapValue = 1; //in my case

     private float Round( float input )
     {
         return snapValue * Mathf.Round( ( input / snapValue ) );
     }

Result:

alt text

Sigh, I always solve my issues 10min after I post on here..

Comment
Add comment · Show 4 · 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 Gorlog · Aug 08, 2016 at 02:52 PM 0
Share

can u post full code here pls i have a same problem ?

avatar image Gorlog · Aug 08, 2016 at 02:56 PM 0
Share

can u post full code here ? im stuck on same problem

avatar image Balddog123 Gorlog · Oct 14, 2020 at 06:58 PM 0
Share
 using UnityEngine;
 
 public class Follow$$anonymous$$ouse : $$anonymous$$onoBehaviour
 {
     public float snapValue = 20;
 
     // Update is called once per frame
     void Update()
     {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, 1 << Layer$$anonymous$$ask.NameToLayer("Tile")))
         {
             Vector3 mousePoint = hit.point;
 
             mousePoint.y = 0f;
             mousePoint.x = Round(mousePoint.x);
             mousePoint.z = Round(mousePoint.z);
 
             transform.position = mousePoint;
         }
         
     }
 
     private float Round(float input)
     {
         return snapValue * $$anonymous$$athf.Round(input / snapValue);
     }
 }
avatar image Balddog123 Balddog123 · Oct 14, 2020 at 07:00 PM 0
Share

This is what I did. Raycast to the world from my mouse position. I'm using Adrian's code to round out the X and Z values of the hit.point from my raycast. Then I set the transform.position to the rounded out Vector3.

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

23 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

Related Questions

Gameobject follow mouse on center 1 Answer

Raycast instantiating object to grid 0 Answers

Check children on a grid 1 Answer

Add object OnMouseDown 1 Answer

Changing a material with a mouse click 2 Answers


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