Hello,
I already have a 2D texture following my mouse. How would you snap that to a grid? For example, I have a background sprite severing as my tiles.
Thanks.
Hello,
I already have a 2D texture following my mouse. How would you snap that to a grid? For example, I have a background sprite severing as my tiles.
Thanks.
You could try something like :
object.transform.postion.x = Mathf.RoundToInt(object.transform.position.x);
Then make your grid with integer increments. That way as you drag, your object should snap to the next integer it rounds up or down to.
Let me know if that works for you. I am looking to implement something like it soon.
I can’t do it. It gives me an error back saying:
Finding isometric tiles under a mouse isn’t as straight forward as a regular square grid.
Check out this thread on Stack Overflow.
It goes over the math and should help you out.
Ah, the error
“Cannot modify a value type return value of `UnityEngine.Transform.position’. Consider storing the value in a temporary variable”
must be because you are writing in C#. UnityScript takes care of this implicitly which is one of the reasons I prefer it. Look up how to set up temporary variables in C# . It’s easy but I’m not very familiar with it.
I tried this technique and added a Lerp for smoothnes in UnityScript, it works great.
heroObject.transform.position.x = Mathf.Lerp(heroObject.transform.position.x, Mathf.RoundToInt(hit.point.x), Time.deltaTime*30);
To modify a position in c# you’ll need to create a new Vector3 to hold it, and then assign that. For example, if you wanted to modify the X value of an objects position…
transform.position = new Vector3(YourXVariableGoesHere, transform.position.y, transform.position.z)
Thank you all for the reply! So here is my current code.
2DMouseMove
using UnityEngine;
using System.Collections;
public class MouseMove : MonoBehaviour {
public Camera myCamera;
private float moveSpeed = 10.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 positionMoveTo = myCamera.ScreenToWorldPoint (Input.mousePosition);
positionMoveTo.z = 0;
transform.position = Vector3.MoveTowards (transform.position, positionMoveTo, moveSpeed * Time.deltaTime);
}
}
That code moves this sprite.
![]()
For the isometric code…
using UnityEngine;
using System.Collections;
public class IsometricSnap : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I want the snapping to be for this

Thank you all.
Did you get it working?
No. I am still stuck with it. I made a temp variable and followed a few of everyone’s answers to it. Still nothing. I don’t know if I’m doing something wrong or don’t know how to interpret the code.
It was mentioned before, but Mathf.RoundToInt() is the key to the functionality that you want I think. You need to clarify the result that you’d like though: do you want the tiles to ONLY be placeable in “snap” locations, or do you want the proximity to an “edge” to snap to it, but ordinarily it doesn’t snap?
In the case of the former, you need to remove the MoveTowards() because you’re going to be assigning fixed values to the object’s position- there will be no lerping because everything is “snapping”. Take the ScreenToWorldPoint result and then replace the X and Y values with rounding results instead like so:
Vector3 positionMoveTo = myCamera.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(Mathf.RoundToInt(positionMoveTo.x), Mathf.RoundToInt(positionMoveTo.y), 0f);
or something like that. If the latter option is what you’d like, the logic is a bit more complex, so I’ll wait for your response first.
Yes! It works, but not for every tile.
Maybe I need to change the value for each of the “positionMoveTo.x” and “positionMoveTo.y”? If so, I can manage that myself. If not, what are your thoughts?
Thanks.
The video is private and unviewable, first off.
Also, I think the problem is that “ScreenToWorldPoint” is giving you coordinate grid values as if a plane were pressed right up against the screen. That’s fine if “right up against the screen” is where you’re snapping your tiles (I don’t know whether that’s the case or not, because no video). More likely, in order to get the coordinates to match up to whatever surface you’re wanting them to snap to, you’re going to need to use a plane.raycast instead, using the camera and ScreenPointToRay (instead of ScreenPointToWorld). Examples can be found on those pages.
Thanks. I fixed the video. Sorry about that. I will look into what you said.