c# - 使可放置对象与网格对齐

标签 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");
        }
    }
}

最佳答案

您可以通过简单地将 transform.position 的每个轴四舍五入来捕捉到代码中的网格:

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

关于c# - 使可放置对象与网格对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184124/

相关文章:

c# - Entity Framework 迁移的默认值如何工作?

c# - 在 Unity 进程和另一个 C# 进程之间执行本地 IPC 的最快方法

c# - 检测用户是否正在从另一个应用程序播放音乐

c# - 统一移动相机的问题

c# - 带删除线的 Unity3d 字体

c# - 如何处理循环引用 - 或者 - 中断在公开 Entity Framework 数据模型的 WCF 服务中返回的第一级子级下的引用?

c# - 如何在 .NET 项目中包含解释器?

c# - 具有多个资源的 "using"会导致资源泄漏吗?

c# - 使用 .NET 4.0 编译器编译 .NET 3.5 项目

c# - 统一 3D : Footstep sounds looping the first few milliseconds instead of playing the full sound then looping