How to place tiles on 2D grid in Unity using mouse position?
<p>I am trying to make a level editor to where the user can click where they want a tile to be placed. Here is the code:</p>

<pre><code>using UnityEngine;
using System.Collections;

public class TilePlacement : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown (0)) {
            var clickPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
            clickPos = Camera.main.ScreenToWorldPoint (clickPos);

            clickPos.z = 0f;
            clickPos.x = Mathf.FloorToInt (clickPos.x);
            clickPos.y = Mathf.FloorToInt (clickPos.y);
            clickPos.x /= 3.2f;
            clickPos.y /= 3.2f;
            clickPos.x = Mathf.Round (clickPos.x);
            clickPos.y = Mathf.Round (clickPos.y);
            clickPos.x *= 3.2f;
            clickPos.y *= 3.2f;

            var go = new GameObject ();
            go.AddComponent&lt;SpriteRenderer&gt; ();
            go.GetComponent&lt;SpriteRenderer&gt; ().sprite = Sprite.Create(new Texture2D(32, 32), new Rect(0, 0, 32, 32), new Vector2(0, 0));
            go.transform.localScale = new Vector3 (10f, 10f, 0f);
            go.transform.position = clickPos;
        }
    }
}
</code></pre>

<p>This code works for placing tiles but the user has to click in the bottom left corner of a tile and the formula for changing clickPos seems too complicated and I think there is a better way to do it. Please help me fix these two problems</p>