Unity3D:找到 Raycastit 方向

Ssi*_*iro 2 c# vector angle unity-game-engine raycasting

我正在尝试制作台球游戏,我想计算主球(白球)击中另一个球后的移动方向。

视觉效果

正如你所看到的,我想计算射线击中球的角度/方向以及射线投射将改变其方向的角度/方向。我需要将角度显示为 Vector3 变量,以便我可以在linerenderer(3) 上使用它。

我已经计算出被击中的球将移动的方向。

如果你能在这方面帮助我那就太好了!

当前代码:

RaycastHit hitz;
if (Physics.SphereCast(transform.position, 0.8f, location - transform.position, out hitz, Mathf.Infinity, lmm2))
{

    lineRenderer2 = hitz.collider.GetComponentInChildren<LineRenderer>();
    lineRenderer2.SetVertexCount(2);

    if (!Input.GetKey(KeyCode.Mouse0))
        lineRenderer2.SetPosition(0, hitz.point);

    if (!Input.GetKey(KeyCode.Mouse0))
       {
           Vector3 start = hitz.point;
           Vector3 end = start + (-hitz.normal * 4);

           if (lineRenderer2)
           {
               if (!Input.GetKey(KeyCode.Mouse0))
                   lineRenderer2.SetPosition(1, end);
           }

           if(lineRenderer3)
           {
               anglelel = Vector3.Angle(hitz.normal, hitz.point);
               Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
               if(cross.y > 0)
               {

                    tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
               }
              if (cross.y < 0)
               {
                  anglelel = -anglelel;
                  tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
               }
               Vector3 start2 = hitz.point;
               Vector3 end2 = start2 + ((tzt) * 5f);
               lineRenderer3.SetPosition(0, hitz.point);
               lineRenderer3.SetPosition(1, end2);
           }
        }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的时间。

编辑:

这部分代码已经改为这一部分,目前取得了一些进展,但仍然不够好。前

if(lineRenderer3)
        {
            Vector3 start2 = hitz.point;
            //THIS IS WHERE I'M CURRENTLY STUCK AT
            Vector3 end2 = start2 + (hitz.point * 0.7f); 
            lineRenderer3.SetPosition(0, hitz.point);
            lineRenderer3.SetPosition(1, end2);
         }
Run Code Online (Sandbox Code Playgroud)

if(lineRenderer3)
{
     anglelel = Vector3.Angle(hitz.normal, hitz.point);
     Vector3 cross = Vector3.Cross(hitz.normal, hitz.point);
     if(cross.y > 0)
     {

          tzt = Quaternion.AngleAxis(90f, hitz.normal) *realStick.transform.forward;
     }
      if (cross.y < 0)
      {
          anglelel = -anglelel;
          tzt = Quaternion.AngleAxis(270f, hitz.normal) * realStick.transform.forward;
       }
       Vector3 start2 = hitz.point;
       Vector3 end2 = start2 + ((tzt) * 5f);
       lineRenderer3.SetPosition(0, hitz.point);
       lineRenderer3.SetPosition(1, end2);
  }
Run Code Online (Sandbox Code Playgroud)

Xan*_*ano 5

让我们一点一点地来看这个。首先,这是一个经典的物理 101 问题。2 个台球撞击时形成的角度是完美的 90 度角。请注意下图中的绿色和蓝色向量如何形成直角:

台球的物理原理

现在,您还应该注意到,从接触点到两个球的中心都垂直于两个球的表面。这意味着,在统一中,我们可以使用hit.normal来获取我们击中的球的行进方向。我们只需要通过执行以下操作来反转它:-1 * hit.normal

现在,为了获得母球行进的方向,我们只需要将之前的向量旋转 90 度即可。我们可以用四元数来做到这一点。我们通过执行以下操作创建围绕向上方向(或与台球桌垂直的任何方向)的 90 度旋转:Quaternion.AngleAxis(-90, Vector3.up)

然后我们可以通过以下方式计算原始行进向量与母球行进角度之间的角度Vector3.Angle(-1 * cue.up, rotate90 * hit.normal)

让我们看一下我的测试场景中的视觉示例:

统一台球桌

我对向量进行统一颜色编码以匹配上图。您可能注意到的唯一区别是黑色向量,它代表我们的hit.normal.

这是代码:

public class Main : MonoBehaviour {
    public Transform cue,cueBallPostHit;
    public int dist = 10;
    public Color red,green,blue;

    RaycastHit hit;
    float scale,ballAngle;
    Quaternion rotate90;
    Vector3 cueBallHitPosition;

    void Start () {
        rotate90 = Quaternion.AngleAxis(-90, Vector3.up);
    }

    void FixedUpdate () {
        if(Physics.SphereCast(cue.position, .5f, cue.up, out hit, dist))
        {
            // Calculate variables
            cueBallHitPosition = hit.point + (.5f * hit.normal);
            scale = (cue.position - hit.point).magnitude;
            ballAngle = Vector3.Angle(-1 * cue.up, rotate90 * hit.normal);
            print(ballAngle);

            // Cue Ball Direction and normal
            Debug.DrawLine(cue.position, cueBallHitPosition, red);
            Debug.DrawRay(cueBallHitPosition, hit.normal, Color.black);
            // Ball direction
            Debug.DrawRay(hit.point + (-.5f * hit.normal), -1 * hit.normal * scale, blue);
            // Cue Ball Direction
            Debug.DrawRay(cueBallHitPosition, rotate90 * hit.normal * scale, green);

            // Visual for where the ball will hit
            cueBallPostHit.position = cueBallHitPosition;
        }
        else
        {
            Debug.DrawRay(cue.position, cue.up * dist, blue);
            cueBallPostHit.position = cue.position + (2 * cue.up);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这足以帮助您朝着正确的方向开始,但如果您有疑问,请告诉我,我将添加更多解释。