如何从 Unity3D 中的第三人称相机的 localEulerAngels 获取 1 到 -1 的浮点值?
How can I get a 1 to -1 float value from the localEulerAngels of a third person camera in Unity3D?
我有一个附有刚体的球,我正在根据第三人称视角(始终注视着球) 围绕球旋转的角度为球增加扭矩。我试图根据摄像机围绕球旋转的位置来改变球的方向。我使用 Vector3.right
表示正向和反向扭矩,Vector3.back
表示左右。我有 forward/reverse 值,rightVal
工作,但我一直在处理 left/right 值,backVal
三天,我不知所措。以下是我尝试实现此目标的方法。
void CalcRightVal()
{
float degrees = Camera.main.transform.localEulerAngles.y;
if (degrees > 0 && degrees < 180.0f)
{
rightVal = (-degrees / 360) * 4 + 1;
}
if (degrees > 180.0f && degrees < 360.0f) //
{
rightVal = (-degrees / 360) * 4 + 1;
if (rightVal < -1.0f)
{
rightVal = (degrees / 360) * 4 - 3;
}
}
}
void ApplyTorque()
{
Vector3 dir = new Vector3(rightVal, 0, backVal);
ball.AddTorque(dir * ballAcceleration);
}
在 ApplyTorque()
中,我需要 backVal
从 0 到 -1,即 Vector3.back
当 degrees == 90
。然后 backVal
的值应该在 degrees == 180
时增加到 0 然后在 degrees == 270
时增加到 1 然后在 degrees == 360
时回到 0。这将在相机处于 90 度时向右施加扭矩,而当相机处于 270 度时向左施加扭矩。这个想法是用户将向前进添加输入,即 KeyCode.W
以添加加速和反向或 KeyCode.S
以进行休息。相机将负责改变力的方向。我的数学并不差,但这个让我打败了。
虽然 rightVal
为 1 或 -1,但 backVal
需要为 0,反之亦然。这样当相机在球的正后方或 0 度时,应用的扭矩将为 Vector3.right
或 (1, 0, 0)
,而当相机在 90 度或在球的左侧时,应用的扭矩将为 Vector3.right
或 (1, 0, 0)
=12=] 或 (0, 0, -1)
。这将施加扭矩使球向右转。基本上无论相机在哪里,球都会从用户的角度向前滚动。感谢您的帮助,我们将不胜感激所有帮助。
这里有一张图片可以提供帮助。
谢谢。
如果您正在寻找平滑的 increase/decrease 反向值,您可以简单地执行以下操作:
backVal = -Mathf.Sin(degrees * Mathf.Deg2Rad);
但是如果你想要一个线性关系,它会稍微复杂一些:
private void CalcBackValue()
{
float degrees = Camera.main.transform.localEulerAngles.z;
if (degrees >= 0 && degrees < 90)
backVal = -degrees / 90f; // 0 to -1
else if (degrees >= 90 && degrees < 270)
backVal = -2 + degrees / 90; // -1 to 0 to 1
else if (degrees >= 270 && degrees < 360)
backVal = 4 - degrees / 90; // 1 to 0
}
您可能需要对此进行扩展以包括负欧拉角,但实际上您是在第二个示例中沿着以下曲线找到值:
我有一个附有刚体的球,我正在根据第三人称视角(始终注视着球) 围绕球旋转的角度为球增加扭矩。我试图根据摄像机围绕球旋转的位置来改变球的方向。我使用 Vector3.right
表示正向和反向扭矩,Vector3.back
表示左右。我有 forward/reverse 值,rightVal
工作,但我一直在处理 left/right 值,backVal
三天,我不知所措。以下是我尝试实现此目标的方法。
void CalcRightVal()
{
float degrees = Camera.main.transform.localEulerAngles.y;
if (degrees > 0 && degrees < 180.0f)
{
rightVal = (-degrees / 360) * 4 + 1;
}
if (degrees > 180.0f && degrees < 360.0f) //
{
rightVal = (-degrees / 360) * 4 + 1;
if (rightVal < -1.0f)
{
rightVal = (degrees / 360) * 4 - 3;
}
}
}
void ApplyTorque()
{
Vector3 dir = new Vector3(rightVal, 0, backVal);
ball.AddTorque(dir * ballAcceleration);
}
在 ApplyTorque()
中,我需要 backVal
从 0 到 -1,即 Vector3.back
当 degrees == 90
。然后 backVal
的值应该在 degrees == 180
时增加到 0 然后在 degrees == 270
时增加到 1 然后在 degrees == 360
时回到 0。这将在相机处于 90 度时向右施加扭矩,而当相机处于 270 度时向左施加扭矩。这个想法是用户将向前进添加输入,即 KeyCode.W
以添加加速和反向或 KeyCode.S
以进行休息。相机将负责改变力的方向。我的数学并不差,但这个让我打败了。
虽然 rightVal
为 1 或 -1,但 backVal
需要为 0,反之亦然。这样当相机在球的正后方或 0 度时,应用的扭矩将为 Vector3.right
或 (1, 0, 0)
,而当相机在 90 度或在球的左侧时,应用的扭矩将为 Vector3.right
或 (1, 0, 0)
=12=] 或 (0, 0, -1)
。这将施加扭矩使球向右转。基本上无论相机在哪里,球都会从用户的角度向前滚动。感谢您的帮助,我们将不胜感激所有帮助。
这里有一张图片可以提供帮助。
如果您正在寻找平滑的 increase/decrease 反向值,您可以简单地执行以下操作:
backVal = -Mathf.Sin(degrees * Mathf.Deg2Rad);
但是如果你想要一个线性关系,它会稍微复杂一些:
private void CalcBackValue()
{
float degrees = Camera.main.transform.localEulerAngles.z;
if (degrees >= 0 && degrees < 90)
backVal = -degrees / 90f; // 0 to -1
else if (degrees >= 90 && degrees < 270)
backVal = -2 + degrees / 90; // -1 to 0 to 1
else if (degrees >= 270 && degrees < 360)
backVal = 4 - degrees / 90; // 1 to 0
}
您可能需要对此进行扩展以包括负欧拉角,但实际上您是在第二个示例中沿着以下曲线找到值: