Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by FourSheds · Dec 30, 2011 at 04:58 PM · positionsnap

Snap to starting position?

Although I know how to snap objects' position when moving them in the Scene (holding down Cmd/Cntrl), unfortunately they snap relative to their starting position, e.g. if x = 1.234, then snap-moving 1m will move it to 2.234. How do you snap them to whole numbers without manually typing in an integer position? I'm surprised this isn't the default behaviour. When you initially drag a piece in to the scene it invariably goes to a non-integer position, which would mean setting its integer position manually every time.

Thanks

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

3 Replies

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

Answer by Rod-Green · Dec 30, 2011 at 10:09 PM

Or as an editor tool

 using UnityEngine;
 using UnityEditor;
 
 
 public class SnapperTool : ScriptableObject
 {
     public Vector3 m_snapValues = Vector3.one;
     
     static SnapperTool m_instance;
     
     static Vector3 GetSnapValues()
     {
         
         if(m_instance == null)
         {
             m_instance = GetInstance();
         }
         
         return m_instance.m_snapValues;
     
     }
     
     static SnapperTool GetInstance()
     {
         if(m_instance != null)
             return m_instance;
         
         SnapperTool[] sceneSnappers = (SnapperTool[])FindSceneObjectsOfType(typeof(SnapperTool));
         SnapperTool sceneSnapper = null;
         if(sceneSnappers.Length != 0)
             sceneSnapper = sceneSnappers[0];
         
         if(sceneSnapper == null)
         {
             sceneSnapper = ScriptableObject.CreateInstance<SnapperTool>();
                sceneSnapper.hideFlags = HideFlags.dontSave;
            }
         m_instance = sceneSnapper;
         return m_instance;
     }
     
     [MenuItem ("Tools/Snapper Settings")]
     static void SnapperSettings ()
     {
         UnityEditor.Selection.objects = new Object[] { GetInstance() };
     }
     [MenuItem ("Tools/Snap")]
     static void DoSnapSelection ()
     {
         if(Selection.gameObjects == null || Selection.gameObjects.Length == 0)
             return;
         
         foreach(GameObject eachGO in Selection.gameObjects)
         {
             SnapGameObject(eachGO);
         }
     }
     
     
     static void SnapGameObject(GameObject p_go)
     {
         Vector3 snapValues = GetSnapValues();
         Vector3 curPos = p_go.transform.localPosition;
         curPos.x = Snap(curPos.x, snapValues.x);
         curPos.y = Snap(curPos.y, snapValues.y);
         curPos.z = Snap(curPos.z, snapValues.z);
         p_go.transform.localPosition = curPos;
     }
     
     
     static float Snap(float p_value, float p_snap)
     {
         if(p_snap <= 0.0f)
             return p_value;
         return (float)Mathf.RoundToInt(p_value / p_snap) * p_snap;
     }
 
 
 }
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 FourSheds · Dec 31, 2011 at 11:40 AM 0
Share

Wow, Rod, thanks so much, a great solution and so much quicker than having to type in values or open the Unity snap settings prefs. I added a key shortcut to the "Tools/Snap &s" also. :) :)

avatar image Rod-Green · Dec 31, 2011 at 06:11 PM 0
Share

Actually one issue with this is it might save the SO with the scene... Let me fix

avatar image
3

Answer by Rod-Green · Dec 30, 2011 at 09:52 PM

As a mono you could do something like

 using UnityEngine;
 [ExecuteInEditMode]
 
 public class Snapper : MonoBehaviour
 {
     public Vector3 m_snapValues = Vector3.one;
     
     float Snap(float p_value, float p_snap)
     {
         if(p_snap <= 0.0f)
             return p_value;
         return (float)Mathf.RoundToInt(p_value / p_snap) * p_snap;
     }
     void Update()
     {
         if(Application.isPlaying)
             return;
         
         Vector3 curPos = transform.localPosition;
         
         curPos.x = Snap(curPos.x, m_snapValues.x);
         curPos.y = Snap(curPos.y, m_snapValues.y);
         curPos.z = Snap(curPos.z, m_snapValues.z);
         
         transform.localPosition = curPos;
         
     }
 
 }
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
avatar image
1

Answer by macweirdo · Dec 30, 2011 at 05:00 PM

You can't, to my knowledge. Just type the position in.

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 FourSheds · Dec 30, 2011 at 09:10 PM 0
Share

Oh, O$$anonymous$$, that's a shame. Thanks.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor Handles Vertex Snap not working on script's object. 0 Answers

Camera rotation around player while following. 6 Answers

Snap object to grid with offset? 1 Answer

Instantiated prefab position 5 Answers

Deviating from path while falling down. 3 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