Unity 3D如何将Vector3投影到平面上或在特定平面中的两个Vector3之间获得角度?

Posted

技术标签:

【中文标题】Unity 3D如何将Vector3投影到平面上或在特定平面中的两个Vector3之间获得角度?【英文标题】:Unity 3D How to project a Vector3 onto a plane or get angle between two Vector3s in a specific plane? 【发布时间】:2020-08-05 15:12:41 【问题描述】:

我正在尝试对第三人称角色进行编程,这样当方向键时,例如。按下 D,如果角色当前面向与相机相同的方向,则播放右四分之一转动画,如果它面向相机,则播放左四分之一转,类似于 GTA V。但是我在获取相机和相机之间的角度时遇到问题Y 平面上的玩家。我在播放器控制脚本中尝试了这个:

void Right()
    
        Vector3 pVec = Vector3.ProjectOnPlane(transform.forward, Vector3.up);
        Vector3 cVec = Vector3.ProjectOnPlane(mainCam.transform.forward, Vector3.up);
        print(cVec);
        float angle = Vector3.Angle(pVec, cVec);
        print(angle);
        if(angle >= 345 && angle <= 15)
        
            animator.Play("StandQuarterTurnRight");
        
        else if(angle >= 255 && angle <= 285)
        
            animator.Play("StandHalfTurnRight");
        
        else if(angle >= 165 && angle <= 195)
        
            animator.Play("StandQuarterTurnLeft");
        
        else if(angle >=75 && angle <= 105)
        
            float forw = Input.GetAxis("Horizontal");
            if (forw > 0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = 0.5f;
            else if (forw < -0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = -0.5f;
            animator.SetFloat("Speed", forw);
        
    

但它不起作用,我得到错误的角度。当角色面向相机前方的右侧或左侧时,我给我 90,而其他人的角度错误,它根本没有给 180+,我做错了什么?有没有更好的方法来实现我正在尝试的目标?

【问题讨论】:

试试三角函数或四元数answers.unity.com/questions/54495/… 投反对票是怎么回事?我的问题有什么问题? 【参考方案1】:

我会将相机的视角转换为角色的坐标系。那么这很容易,所以看看你想让他转向或移动的地方。

假设您的相机旋转在mainCam.transform.rotation,您可以使用以下代码:

float target_angle = 90.0f; // assuming want to turn 'right'.

// direction of camera, in worldspace.
Vector3 cam_dir = mainCam.transform.forward;
// now transform to a direction vector in character's local space.
cam_dir = transform.InverseTransformDirection(cam_dir);
// ignore y part, take X/Z to get the angle.
// 0 degrees is forward, 90 deg is toward positive X, so normally right.
float cam_angle = Mathf.Atan2(cam_dir.x,cam_dir.z)*Mathf.Rad2Deg;

// angle we need to turn
float turn_angle = target_angle - cam_angle;
// [.....] do it now.

如果尝试在某些角色的本地视图中思考,使用 InverseTransformXxx() 函数通常非常有用。您还可以使用char.transform.InverseTransformPoint(mainCam.transform.position) 将相机的位置转换为角色的空间,并将其用作参考。

【讨论】:

问题是我的相机的前方不在Y播放器中,相机略高于播放器并且向下倾斜,所以测量的角度是3D的。我想要 Y 平面上的角度 这就是它的作用。它将相机视图转换为播放器的局部空间,其中仍然包含 Y 上的一些负分量。连同 X 和 Z。请参阅评论 ignore y part, ...。将 Y 放在那里会将投影的相机方向向量展平到 Y 平面。 我喜欢这种方法。 @ShantanuShinde 请看meta.stackexchange.com/a/5235/405359【参考方案2】:

这是对您的代码的一个小修复,它可以调整您的结果以使其正常工作,而无需更改任何其他内容:

void Right()

    Vector3 pVec = Vector3.ProjectOnPlane(transform.forward, Vector3.up);
    Vector3 cVec = Vector3.ProjectOnPlane(mainCam.transform.forward, Vector3.up);
    print(cVec);

    float angleA = Vector3.Angle(pVec, cVec); //Get the angle between the 2 vectors, projected on Y-plane
    float perspectiveAngle = Vector3.Angle(transform.right, cVec); //Get the angle between camera and player's right vector
    float angle = angleA; //In case angle is below 180, angle is AngleA

    if (perspectiveAngle > 90f) //If angle between player's right vector and camera is > 90, then we need to adjust the angle, as it is equal to or greater than 180
        angle = 360f - angleA;

    print(angle);
    if (angle >= 345 && angle <= 15)
    
        animator.Play("StandQuarterTurnRight");
    
    else if (angle >= 255 && angle <= 285)
    
        animator.Play("StandHalfTurnRight");
    
    else if (angle >= 165 && angle <= 195)
    
        animator.Play("StandQuarterTurnLeft");
    
    else if (angle >= 75 && angle <= 105)
    
        float forw = Input.GetAxis("Horizontal");
        if (forw > 0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = 0.5f;
        else if (forw < -0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = -0.5f;
        animator.SetFloat("Speed", forw);
    

请参阅上面的 cmets,其中描述了它的工作原理。希望对您有所帮助

【讨论】:

【参考方案3】:

我建议不要在这里尽可能使用角度。相反,您可以使用相机右侧和玩家本地方向之间的点积来确定哪个本地方向与相机右侧方向最一致。 cmets 中的解释。

void Right()

    float dotThreshold = Mathf.Sin(Mathf.PI * 0.25f); 

    // Take the dot products between the camera's right and  
    // each direction from the player. 
    // Either exactly one dot product will exceed this threshold 
    // (sin 45 degrees) or two will equal it.
    // Either way, when we see one dot product >= the threshold, 
    // we know what direction we should face.

    Vector3 camRight = mainCam.transform.right;

    if(Vector3.Dot(camRight, transform.right) >= dotThreshold) 
    
        // camera's right ~ player's right
        animator.Play("StandQuarterTurnRight");
    
    else if(Vector3.Dot(camRight, -transform.forward) >= dotThreshold) 
    
        // camera's right ~ player's back
        animator.Play("StandHalfTurnRight");
    
    else if(Vector3.Dot(camRight, -transform.right) >= dotThreshold)
    
        // camera's right ~ player's left
        animator.Play("StandQuarterTurnLeft");
    
    else
    
        // camera's right ~ player's forward
        float forw = Input.GetAxis("Horizontal");
        if (forw > 0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = 0.5f;
        else if (forw < -0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = -0.5f;
        animator.SetFloat("Speed", forw);
    

如果您不能假设玩家和相机具有相同的 y 轴方向,则必须像问题中所做的那样投影到同一平面:

void Right()

    float dotThreshold = Mathf.Sin(Mathf.PI * 0.25f); 

    // Take the dot products between the camera's right and  
    // each direction from the player. 
    // Either exactly one dot product will exceed this threshold 
    // (sin 45 degrees) or two will equal it.
    // Either way, when we see one dot product >= the threshold, 
    // we know what direction we should face.

    Vector3 camRight = Vector3.ProjectOnPlane(mainCam.transform.right, Vector3.up);
    Vector3 playerRight = Vector3.ProjectOnPlane(transform.right, Vector3.up);
    Vector3 playerForward = Vector3.ProjectOnPlane(transform.forward, Vector3.up);

    if(Vector3.Dot(camRight, playerRight) >= dotThreshold) 
    
        // camera's right ~ player's right
        animator.Play("StandQuarterTurnRight");
    
    else if(Vector3.Dot(camRight, -playerForward) >= dotThreshold) 
    
        // camera's right ~ player's back
        animator.Play("StandHalfTurnRight");
    
    else if(Vector3.Dot(camRight, -playerRight) >= dotThreshold)
    
        // camera's right ~ player's left
        animator.Play("StandQuarterTurnLeft");
    
    else
    
        // camera's right ~ player's forward
        float forw = Input.GetAxis("Horizontal");
        if (forw > 0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = 0.5f;
        else if (forw < -0.5f && !Input.GetKey(KeyCode.LeftShift)) forw = -0.5f;
        animator.SetFloat("Speed", forw);
    

【讨论】:

以上是关于Unity 3D如何将Vector3投影到平面上或在特定平面中的两个Vector3之间获得角度?的主要内容,如果未能解决你的问题,请参考以下文章

使用unity3d实现2d游戏有几种方式

unity3d中如何让物体从一个点自动移动到另一个点呢,

随笔-Unity中Vector3的点乘叉乘投影等的几何意义及应用

[Unity3D] Vector3.Lerp(x, y, Time.deltaTime) 是什么意思?

Unity3D之Vector3.Dot和Vector3.Cross的使用

如何在 Unity 中从 3D 对象中获取 2D 视图/纹理