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



我正在尝试对第三人称角色进行编程,这样当按下方向键(例如D(时,如果角色当前面向与相机相同的方向,则播放"右四分之一转弯"动画,如果角色面向相机,则播放《左四分之一转》,类似于《侠盗猎车手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+,我做错了什么?有没有更好的方法来实现我正在努力的目标?

我会将相机的视角转换为角色的坐标系。然后这很容易,所以看看你想让他转向或移动到哪里。

假设你的相机的旋转是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)将摄影机的位置变换到角色的空间中,并将其用于某些参考。

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

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);
}
}

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

我建议尽量不要在这里使用角度。相反,您可以在相机的右侧和玩家的局部方向之间使用点积来确定哪个局部方向最符合相机的右侧方向。注释中的解释。

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);
}
}

相关内容

  • 没有找到相关文章

最新更新