【问题标题】:Unity angle and position relative to direction相对于方向的统一角度和位置
【发布时间】:2017-12-26 13:58:07
【问题描述】:

我正在尝试围绕一个点创建一个向量点的“星形”,它们之间的角度与源点和命中点之间的原始线(见图)之间的角度恒定(参见图片),这是我通过创建新向量来完成的与原始版本的小偏移:

private void FixedUpdate()
{
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit, 3000))
    {

    Vector3 mousePos = hit.point;
    Debug.DrawLine(transform.position, hit.point, Color.yellow);

    Vector3[] explorePoints = new Vector3[6] {
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z + 1), // diag left
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z + 1), // diag right
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z), // left
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z), // right
                new Vector3 (hit.point.x - 1 , hit.point.y, hit.point.z - 1), // diag left back
                new Vector3 (hit.point.x + 1 , hit.point.y, hit.point.z - 1), // diag right back
    };

    for (int x = 0; x < explorePoints.Length; x++)
    {
        Debug.DrawLine(mousePos, explorePoints[x], Color.red);
    }

}
}

当鼠标之间的角度接近 0 或 180 时,这可以正常工作,但在其他角度当然不行:

我知道我可能需要 Quaternion 类将球体和鼠标点之间的角度应用于方向向量,但不能完全弄清楚,例如

Quaternion q = Quaternion.FromToRotation(transform.position, mousePos);
for (int x = 0; x < explorePoints.Length; x++)
{
      Debug.DrawLine(mousePos, q * explorePoints[x], Color.red);
}

如何让红线始终与黄线成 n 角?

【问题讨论】:

    标签: c# unity3d unity5 vectormath


    【解决方案1】:

    private void FixedUpdate()
      {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit, 3000))
        {
          Vector3 mousePos = hit.point;
          Debug.DrawLine(transform.position, hit.point, Color.yellow);
          Vector3 rayDir = transform.position - mousePos;
          Vector3[] explorePoints = new Vector3[6] {
            Quaternion.Euler(0, 0, 45) * rayDir.normalized,
            Quaternion.Euler(0, 0, 90) * rayDir.normalized,
            Quaternion.Euler(0, 0, 135) * rayDir.normalized,
            Quaternion.Euler(0, 0, -45) * rayDir.normalized,
            Quaternion.Euler(0, 0, -90) * rayDir.normalized,
            Quaternion.Euler(0, 0, -135) * rayDir.normalized,
          };
    
          float starLength = 100;
          for (int x = 0; x < explorePoints.Length; x++)
          {
            // we want to use the vector as DIRECTION, not point, hence mousePos + explorePoints[x] (starLength is just the length of the red line)
            Debug.DrawLine(mousePos, mousePos + (explorePoints[x] * starLength), Color.red);
          }
        }
      }

    【讨论】:

    • 谢谢,不完全是,当您移动鼠标点时,角度不是恒定的。我想我需要计算出方向(transform.position - hit.point),然后创建一个与该方向成角度的向量,我无法解决的问题是如何保持该角度相对于方向,而不是世界空间。有什么想法吗?
    • 你试过代码了吗?它将您的矢量旋转 60 度,而不是 60 度
    • 是的,还有几个变体。如果你有时间自己试试,你会明白我的意思。
    • @Absinthe 哦,我想我看到了问题所在。查看我更新的答案(注意 for 循环中的注释)
    • 啊,我没想到要添加 mousePos(愚蠢地我忘了提到我的视图是自上而下的,所以角度围绕 Y 线)。太棒了,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多