• 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
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

· Add your reply
  • Sort: 
avatar image
4
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(MeshRenderer))]
  
  
 public class MouseDrag : MonoBehaviour {
      
     
     Vector3 screenPoint;
     Vector3 offset;
     float yPos;
 
     
     void OnMouseDown() {
         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 OnMouseDrag() {
         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(MeshRenderer))]
  
  
 public class MouseDrag : MonoBehaviour {
      
     
     Vector3
             screenPoint,
             offset,
             scanPos,
             curPosition,
             curScreenPoint;
 
     
     float
             gridSize = 0.20f;
 
 
     
     void OnMouseDown() {
         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 OnMouseDrag() {
         curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
     
         
         curPosition.x = (float)(Mathf.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 xproject2013 · Aug 08, 2013 at 04:16 PM 0
Share

Could you write a code example?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Snap to grid after click and drag? 2 Answers

Snap to grid edge midpoints? 1 Answer

Is there a way to check if something was snapped on a specific grid? 1 Answer

Create Grid and Snap Rigidbodies to Grid 1 Answer

An UI element will follow the mouse and snap to a grid, but why is the grid shifted after every game start? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges