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 /
This question was closed Jan 16, 2016 at 04:20 PM by LockBros for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by LockBros · Jan 16, 2016 at 04:03 PM · mouseaxisgridsnapgridmove

Snap to grid

I have the following code that works, however it will not snap both X and Z but only the X or only the Z depending on what the last line of code is.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(BoxCollider))]
 [RequireComponent(typeof(MeshRenderer))]
 
 
 public class MouseDrag : MonoBehaviour {
 
 
     
     
     Vector3
         screenPoint,
         scanPos,
         curPositionx,
         curPositionz,
         curScreenPoint;
     
     
     public float
         gridSize = 1;
     
     
     
     void Awake() {
         scanPos = gameObject.transform.position;
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
 
     }
     
 
     
     
     void Update() {
 
 
         curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
         curPositionz = Camera.main.ScreenToWorldPoint (curScreenPoint);
 
         
 
         curPositionx.x = (int)(Mathf.Round (curPositionx.x) * gridSize);
         curPositionz.z = (int)(Mathf.Round (curPositionz.z) * gridSize);
 
     
     
         
 
         transform.position = curPositionz;
         transform.position = curPositionx;
 
 
 
     }
     
     
 }


If the last line is as above then it will snap on the X axis but not the Z.

If the last 2 line of code are reversed then the Z axis will snap and not the X axis.

Is there away to combine them both together? or am I looking at it the wrong way?

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by bubzy · Jan 16, 2016 at 04:14 PM

you are kinda going about it the wrong way

      transform.position = curPositionz;
      transform.position = curPositionx;

with these two lines of code being consecutive, unity will first assign curPositionz to transform.position, IMMEDIATELY afterwards it will assign curPositionx to transform.position

this will have the effect of only the last line of code actually making a visible change.

it seems strange that you are storing the values in the way that you are, you would be better off having

          Vector3 curPosition;
 
          curPosition.x = Camera.main.ScreenToWorldPoint (curScreenPoint);
          curPosition.z = Camera.main.ScreenToWorldPoint (curScreenPoint);
  
          
  
          curPosition.x = (int)(Mathf.Round (curPosition.x) * gridSize);
          curPosition.z = (int)(Mathf.Round (curPosition.z) * gridSize);
 
          transform.position = curPosition;


this would tidy up the code a lot. of course if you want to store x and z in different vector3's you could change the last line of code (replacing the 2 you have now) to :

transform.position = new Vector3(curPositionx.x,transform.position.y,curPositionz.z);

this would also work

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 LockBros · Jan 16, 2016 at 04:18 PM 0
Share

You have completely solved my issue. with:

 curPosition.x = (int)($$anonymous$$athf.Round (curPosition.x) * gridSize);
           curPosition.z = (int)($$anonymous$$athf.Round (curPosition.z) * gridSize);
  
           transform.position = curPosition;


I needed to simple make them as one variable rather than having 2 separate.

Thank you.

avatar image bubzy LockBros · Jan 16, 2016 at 04:50 PM 1
Share

no problem, sometimes the simple solution is the hardest to spot :)

avatar image
1

Answer by hexagonius · Jan 16, 2016 at 04:16 PM

 void Update() {
  
  
          curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
          curPositionx = Camera.main.ScreenToWorldPoint (curScreenPoint);
  
          curPositionx.x = (int)(Mathf.Round (curPositionx.x) * gridSize);
          curPositionx.z = (int)(Mathf.Round (curPositionx.z) * gridSize);
 
          transform.position = curPositionx;
      }
Comment
Add comment · Show 1 · 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 LockBros · Jan 16, 2016 at 04:20 PM 0
Share

Thank you for your help. This is exactly what I had to change.

Follow this Question

Answers Answers and Comments

39 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

Related Questions

Highlighting all clones instead of the one the mouse is over. 0 Answers

ProBuilder Element Snap to Grid (Unity 2020.1.2f1) 0 Answers

snap object to gridwise 0 Answers

Need to make grid movement more smooth 1 Answer

Is ther a way to base movement translation through the grid map of the tilemap? 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