使可放置对象对齐网格

时间:2019-03-15 13:51:07

标签: c# unity3d

我的游戏中有一个构建机制,我想知道如何做到这一点,所以每当我将对象放下时,它都会捕捉到网格。每当我按0时,它将对象移动到鼠标上,而当我单击时,将对象向下放置。我想在放置时使其对齐网格,以免对象碰撞。

using UnityEngine;
using UnityEngine.AI;

public class GroundPlacementController : MonoBehaviour
{
    [SerializeField]
    private GameObject placeableObjectPrefab;
    public NavMeshObstacle nav;


    [SerializeField]
    private KeyCode newObjectHotkey = KeyCode.A;
    
    private GameObject currentPlaceableObject;


    private float mouseWheelRotation;

    



    private void Update()
    {
        HandleNewObjectHotkey();
        nav = GetComponent<NavMeshObstacle>();
        if (currentPlaceableObject != null)
        {
            MoveCurrentObjectToMouse();
            RotateFromMouseWheel();
            ReleaseIfClicked();



        }
    }

    private void HandleNewObjectHotkey()
    {
        if (Input.GetKeyDown(newObjectHotkey))
        {
            if (currentPlaceableObject != null)
            {
                Destroy(currentPlaceableObject);
               
            }
            else
            {
                currentPlaceableObject = Instantiate(placeableObjectPrefab);


            }
        }

    }
    

    private void MoveCurrentObjectToMouse()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hitInfo;
        if (Physics.Raycast(ray, out hitInfo))
        {
            currentPlaceableObject.transform.position = hitInfo.point;
            currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
            currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = false;


        }
    }

    private void RotateFromMouseWheel()
    {
        Debug.Log(Input.mouseScrollDelta);
        mouseWheelRotation += Input.mouseScrollDelta.y;
        currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * 90f);
    }

    private void ReleaseIfClicked()
    {
        if (Input.GetMouseButtonDown(0))
        {
            
            currentPlaceableObject.GetComponent<NavMeshObstacle>().enabled = true;
            print("disabled");
            currentPlaceableObject = null;
            print("removed prefab");
        }
    }
}

2 个答案:

答案 0 :(得分:4)

您只需将transform.position的每个轴四舍五入即可在代码中对齐网格:

var currentPosition = transform.position;
transform.position = new Vector3(Mathf.Round(currentPosition.x),
                                 Mathf.Round(currentPosition.y),
                                 Mathf.Round(currentPosition.z));

答案 1 :(得分:2)

一种更数学的方法,并且您可以轻松设置栅格大小的方法(除了Lews Therin的答案)将是:

  1. 采用position mod yourGridSize(例如,您的网格大小为64,位置为144。然后:144 mod 64 = 16)
  2. mod结果并从位置中减去144-16 = 128
  3. 最后,将以上结果除以yourGridSize:128/64 = 2

现在您知道自己的位置是2格到网格中。对三个轴应用此操作:

var yourGridSize = 64;

var currentPosition = currentPlaceableObject.transform.position;
currentPlaceableObject.transform.position = new Vector3(((currentPosition.x - (currentPosition.x % yourGridSize)) / yourGridSize) * yourGridSize,
                                                        ((currentPosition.y - (currentPosition.y % yourGridSize)) / yourGridSize) * yourGridSize,
                                                        ((currentPosition.z - (currentPosition.z % yourGridSize)) / yourGridSize) * yourGridSize);

令人费解?也许。有效? 哎呀!