简体   繁体   English

如何使用鼠标位置在Unity中的2D网格上放置图块?

[英]How to place tiles on 2D grid in Unity using mouse position?

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: 这是代码:

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<SpriteRenderer> ();
            go.GetComponent<SpriteRenderer> ().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;
        }
    }
}

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. 该代码可用于放置图块,但用户必须单击图块的左下角,并且更改clickPos的公式似乎过于复杂,我认为有更好的方法来实现。 Please help me fix these two problems 请帮我解决这两个问题

Are you working in 2D with an orthographic camera? 您正在使用正交摄影机进行2D工作吗? In 3D I would do it like this, which also works for 2D planes. 在3D中,我会像这样进行操作,它也适用于2D平面。

Plane _groundPlane;

void Start()
{
    _groundPlane = new Plane(Vector3.up, Vector3.zero);
}

void Update()
{
    if (Input.GetMouseButtonDown(0) == false)
        return;


    int x = 0;
    int z = 0;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    float distance;
    if (_groundPlane.Raycast(ray, out distance))
    {
        Vector3 worldPosition = ray.GetPoint(distance);
        x = Mathf.RoundToInt(worldPosition.x);
        z = Mathf.RoundToInt(worldPosition.z);

        Debug.DrawLine(Camera.main.transform.position, worldPosition);
        Debug.LogFormat("Clicked positions: {0} | {1}", x, z);
    }
}

I create a Plane that lies flat with the default grid. 我创建平放使用默认的网格 Then I do a raycast against it to find the hit point from the mouse screen position in world space. 然后,我对它进行光线投射,以从世界空间中的鼠标屏幕位置中找到击中点。 I also made each cell one unit in size, so that I can round to int to get the index position, which also works as the world transform position. 我还使每个单元格的大小为一个单位,以便我可以舍入到int以获取索引位置,该位置也可用作世界变换位置。

If you want to save the raycast and don't need a 3D camera perspective, you can simply get the mouse world position like you are already doing. 如果您想保存光线投射并且不需要3D相机透视图,则可以像已经在做的那样简单地获取鼠标的世界位置。 If you make each tile one unit big, everything will just work when rounded to int. 如果将每个磁贴大一个单位,则四舍五入为int时,一切都将正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM