【问题标题】:Rotate an object according to terrain in Unity (C#)在 Unity (C#) 中根据地形旋转对象
【发布时间】:2014-12-02 15:09:34
【问题描述】:

我目前有一个用于建造的物品放置系统。它通过实例化对象的“幽灵”来显示它可以放置的位置,幽灵(半透明)对象连接到相机并在玩家点击时在其位置实例化对象。

我得到了保持幽灵对象的位置,如下所示:

        var pos = transform.position + transform.forward * placeDistance;  // A position 'someDistance' in front of the player
        pos.y = Terrain.activeTerrain.SampleHeight(pos);  // Get the position at the surface of the terrain to place the object
        firePlaceable.transform.position = pos + Vector3.up * 0.001f;  // 'halfHeight' is used in case the pivot is not on the base

现在.. 我需要对象根据地形旋转,以便放置火场或多或少正确旋转。有任何想法吗?最好的计划是什么?

【问题讨论】:

    标签: c# unity3d rotation building


    【解决方案1】:

    使用该地点的地形法线向量。

    例如,您可以直接从壁炉向下进行光线投射。生成的命中包含一个法线,即您所在位置的向上向量。

    考虑一下...我假设您已经进行了光线投射以获得正确放置壁炉的位置?

    使用放置 raycast 获取向上向量,而不是创建一个新向量。

    基本上是这样

    fireplace.transform.up = clickPlaceHit.normal;
    

    【讨论】:

    • 非常感谢!!这就是我所做的:Ray ray = new Ray(pos + Vector3.up * 10.0f, Vector3.down); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit)) { firePlaceable.transform.rotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation; }
    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多