RTS building snap to grid

Hi I am trying to figure out how to get my building to snap to a grid when I move it around. Right now it follows my mouse position just fine, but I can’t get it to snap. Please help!

    private Vector3 temp;

    // Update is called once per frame
    void Update () {

        
        temp.x = Mathf.Round(Input.mousePosition.x);
        temp.y = Mathf.Round(Input.mousePosition.y);
        temp.z = Camera.main.transform.position.y - 0.5f;


        transform.position = Camera.main.ScreenToWorldPoint(temp);

I just need the x and y to snap, the z position is just fine. Thanks! :slight_smile:

Try this :

Vector3 finalPos = Vector3.zero;
void Update(){
     Vector3 cursorPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     finalPos = new Vector3(Mathf.Round(cursorPos.x), Mathf.Round(cursorPos.y), [Z value here]);
}

Finally got the snapping to work using this code

        Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y));
        Vector3 finalPos = new Vector3(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y), Mathf.Round(mousePos.z));
        transform.position = finalPos;

This is all in void Update()