The question is answered, right answer was accepted
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?
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
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.
no problem, sometimes the simple solution is the hardest to spot :)
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;
}
Thank you for your help. This is exactly what I had to change.
Follow this Question
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