- Home /
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
this question is the same question im asking in my head
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);
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;
}
}
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));
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?
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.
Try the first method I suggested:
curPosition.x = ((int)(x/gridSize))*gridSize
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
Your answer
Follow this Question
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