【问题标题】:Why is my raycast hit point expected output different to the actual output?为什么我的光线投射命中点预期输出与实际输出不同?
【发布时间】:2021-01-26 21:28:12
【问题描述】:

我一直在尝试使用反向运动学制作角色控制器,并且我正在使用射线投射来确定骨骼要附加到的目标点,但是我在更改射线投射命中信息时遇到了一些问题。

到底发生了什么? 好吧,我正在向光线投射点所面对的方向发射光线投射。一旦它撞到地面,它将锚点位置设置为命中点位置。除了我的锚点位置与我的命中点位置完全不同,即使在光线投射之后。

public Transform[] anchors;
void RayOut()
{
    RaycastHit hit; // Create Ray Instance
    if (Physics.Raycast(transform.position, transform.forward, out hit)) // Launch The Instance
    {
        Debug.DrawLine (transform.position, hit.point);
        switch (gameObject.name) // Check This Name & Assign Positions
        {
            case "TR":
                anchors[0].position = hit.transform.position;
                break;
            
            case "TL":
                anchors[1].position = hit.transform.position;
                break;
            
            case "BL":
                anchors[2].position = hit.transform.position;
                break;
            
            case "BR":
                anchors[3].position = hit.transform.position;
                break;
        }
    }
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
        RayOut();
}

白线与地板相交的点是锚点应该在的预期点,但三个箭头是实际位置。

【问题讨论】:

  • 也许你忘了改造一些东西?
  • @aybe 你这是什么意思?
  • 基于docspoint 是射线撞击碰撞器的位置,而transform 是撞击碰撞器变换的位置。
  • @ps2goat 是的,我和你同时想出来的! XD

标签: c# unity3d


【解决方案1】:

我找到了原因,我使用的是transform.position,而实际上它应该是point。 (感谢 ps2goat 也发现了它)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-01
    相关资源
    最近更新 更多