Search code examples
c#unity-game-enginemouseeventroundingupdating

Rounding position with mouse constant update FIXED


I'm making a mario maker style game in C# (unity) where everything has to snap to a grid. I want to, while I'm dragging it, have the item snap into place closest to each int.

 void FixedUpdate () {
      Vector2 pos = Input.mousePosition;
      pos.x = Mathf.Round(pos.x);
      pos.y = Mathf.Round(pos.y);
      pos = Camera.main.ScreenToWorldPoint (pos);
      transform.position = pos;
 }

This does not work for me, and I've been trying for hours to fix this. Nothing has helped so far, I've gone through every forum I could find to no avail. Please help!!


Solution

  • I think you should round values after changing pixel pos to world pos. I mean, after pos = Camera.main.ScreenToWorldPoint (pos);

    Let me know result. If it not work, I would think another try.