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
0
Question by xproject2013 · Aug 06, 2013 at 11:24 AM · griddragsnapsnapping

How to drag gameobject (only x y) snapped to a grid?

Hi, i would like to know how to drag an object with the mouse only to the X and Y axis.

Most important i need this object snapped to a grid (of 2 unit for example), the pivot point of the game object is at left bottom.

During the drag when the object is close enought to the next grid point I needt that the object move to the next point. I fount this wiki but it works different:

http://wiki.unity3d.com/index.php?title=GridMove

Tnanks in advance

Comment
Add comment · Show 1
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 epicraisin · Feb 08, 2020 at 06:23 PM 0
Share

this question is the same question im asking in my head

2 Replies

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

Answer by TonyLi · Aug 06, 2013 at 02:59 PM

At edit time or run time?

At edit time, set the Snap Settings as described in Positioning GameObjects.

At run time, keep track of the world position, but round it up or down to the nearest unit size. The example below rounds to one unit:

     Vector3 worldPos = <smooth position in world>
     transform.position = new Vector3(Mathf.Round(worldPos.x), Mathf.Round(worldPos.y), worldPos.z);


Comment
Add comment · Show 13 · 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 xproject2013 · Aug 07, 2013 at 07:34 AM 0
Share

Hi, this is the code that i use for the drag, could you show me how to implement your code?

I need this working on run time when i move the object

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(BoxCollider))]
 [RequireComponent(typeof($$anonymous$$eshRenderer))]
  
  
 public class $$anonymous$$ouseDrag : $$anonymous$$onoBehaviour {
      
     
     Vector3 screenPoint;
     Vector3 offset;
     float yPos;
 
     
     void On$$anonymous$$ouseDown() {
         Vector3 scanPos = gameObject.transform.position;
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
         //Debug.Log (Input.mousePosition.x);
         
         
         offset = scanPos - Camera.main.ScreenToWorldPoint(
         new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
     }
  
  
     void On$$anonymous$$ouseDrag() {
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
         
         //Debug.Log(curPosition[0]);
         
             transform.position = curPosition;
     }
     
  
 }
avatar image TonyLi · Aug 08, 2013 at 12:40 AM 0
Share

In Unity, the X axis is generally left-to-right, the Y axis is down-to-up, and the Z axis is back-to-front.

If you're placing units on a flat world (like a strategy game map), you want the units to move on the X-Z plane, with a constant Y (probably Y=0). To do this, I recommend using Camera.ScreenPointToRay to see what point on the map the ray intersects. Then just round to whatever your scale is -- for example, if each square is 10 world units:

     x = ((int)(x/10))*10
     z = ((int)(z/10))*10
     y = 0

If you really want to place units on the X-Y plane, like they're on a wall (really the inside of a sphere) where all the points are equally far (Z distance) from the camera, then use Camera.ScreenToWorldPoint:

     worldPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, zDistanceFromCamera));


avatar image xproject2013 · Aug 08, 2013 at 03:17 PM 0
Share

Hi thanks for the answer, im close to reach what i need with the code below:

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(BoxCollider))]
 [RequireComponent(typeof($$anonymous$$eshRenderer))]
  
  
 public class $$anonymous$$ouseDrag : $$anonymous$$onoBehaviour {
      
     
     Vector3
             screenPoint,
             offset,
             scanPos,
             curPosition,
             curScreenPoint;
 
     
     float
             gridSize = 0.20f;
 
 
     
     void On$$anonymous$$ouseDown() {
         scanPos = gameObject.transform.position;
         screenPoint = Camera.main.WorldToScreenPoint(scanPos);
         offset = scanPos - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
         
     }
     
 
  
  
     void On$$anonymous$$ouseDrag() {
         curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     
         
         curPosition.x = (float)($$anonymous$$athf.Round(curPosition.x) * gridSize);
         
            transform.position = curPosition;
     }
 
  
 }



Now the object follow a grid snap, but the mouse cursor is too far away from the object when i make large move on the screen, have any idea in order to resolve this issue?

avatar image TonyLi · Aug 08, 2013 at 03:42 PM 0
Share

If the Game view isn't fullscreen, you can use ViewportToWorldPoint and clip the mouse input values. Or just clamp screenPoint.z to a maximum value. Hope that helps.

avatar image TonyLi · Aug 09, 2013 at 06:16 PM 1
Share

Try the first method I suggested:

     curPosition.x = ((int)(x/gridSize))*gridSize
Show more comments
avatar image
0

Answer by xproject2013 · Aug 10, 2013 at 02:07 PM

Exactly! if you try to replace the variables with the values ​​you will see that the operation is correct

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

17 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

Related Questions

Gizmo won't stop snapping to grid. 1 Answer

Snap object to grid with offset? 1 Answer

RTS building snap to grid 2 Answers

Snap All Axes Hotkey? 0 Answers

Snapping to Grids 0 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