[英]Smoothly Rotate Object Towards Target Object
I want to rotate my player vehicle into the target object direction/side. 我想将玩家的载具旋转到目标物体的方向/侧面。 Though the following image, I have tried to explain my point in better way:
尽管如下图,但我试图以更好的方式解释我的观点:
I want to rotate my below tank object towards another tank object so that it can point into that direction. 我想将我的下方坦克对象朝向另一个坦克对象旋转,以便它可以指向该方向。
I have written this code for this purpose but it is not working: 我已经为此目的编写了此代码,但是它不起作用:
IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("do rotation");
Vector3 targetDirection = opponentPlayer.position - transform.position;
targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion nextRotation = Quaternion.Lerp(transform.localRotation, targetRotation, Time.deltaTime);
transform.localRotation = nextRotation;
yield return null;
} while (Quaternion.Angle(transform.localRotation, targetRotation) < 0.01f);
}
I just want to smoothly rotate and stop towards a target object. 我只想平稳地旋转并停向目标对象。 Please share your suggestion regarding this.
请分享您对此的建议。
EDIT: 编辑:
This is updated code that still not working, tank object gets stuck in rotaion as like above image: 这是更新后的代码,仍然无法正常工作,坦克对象陷入了旋转状态,如上图所示:
IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("do rotation");
Vector3 targetDirection = (opponentPlayer.position - transform.position).normalized;
targetRotation = Quaternion.LookRotation(targetDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);
yield return null;
} while (Quaternion.Angle(transform.rotation, targetRotation) < 0.01f);
}
Tank object forward direction: 坦克物体前进方向:
Time.deltaTime
is an inappropriate lerp parameter. Time.deltaTime
是不合适的lerp参数。 Instead, define a float rotationSpeed
and use rotationSpeed * Time.deltaTime
as the maxDegreesDelta
argument to Quaternion.RotateTowards
. 而是定义一个
float rotationSpeed
并使用rotationSpeed * Time.deltaTime
作为Quaternion.RotateTowards
的maxDegreesDelta
参数。
Also, using LookRotation
in that way will give you a world-space rotation. 同样,以这种方式使用
LookRotation
将为您提供世界空间的旋转。 So you should assign your result to transform.rotation
instead of transform.localRotation
. 因此,您应将结果分配给
transform.rotation
而不是transform.localRotation
。
Altogether, these changes might look like this: 总而言之,这些更改可能看起来像这样:
public float rotationSpeed;
IEnumerator DoRotationAtTargetDirection(Transform opponentPlayer)
{
Quaternion targetRotation = Quaternion.identity;
do
{
Debug.Log("do rotation");
Vector3 targetDirection = opponentPlayer.position - transform.position;
targetRotation = Quaternion.LookRotation(targetDirection);
Quaternion nextRotation = Quaternion.RotateTowards(
transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
transform.rotation = nextRotation;
yield return null;
} while (Quaternion.Angle(transform.rotation, targetRotation) > 0.01f);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.