我怎么知道当Vector3.Rotate.对完成了吗?


void Update()
{
if (playOnce == false)
{
Vector3 targetDirection = target.position - transform.position;
float singleStep = rotationSpeed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
private void OnTriggerEnter(Collider other)
{
if (playOnce)
{
animator.SetTrigger("Picking Up");
playOnce = false;
}
}

问题是它在transform旋转到目标之前播放动画,我希望变换会朝着目标旋转,如果需要的话,也会向目标移动一点,然后开始动画,这样当拾取动画播放时,它会尽可能地看起来自然,因为现在看起来变换(玩家)正在拾取目标附近的东西,并直接拾取目标本身。我认为它应该看起来像手或玩家从中间或多或少地拿起物体。

拾取示例

如果我理解正确的话,您希望在拾取动画播放之前将对象完全旋转到它的目标。然后,你可以将目标的信息存储在OnTriggerEnter中,并在更新循环中执行所有操作。

Transform _target;
OnTriggerEnter(Collider other)
{
_target = other.transform;
}

你需要将playOnce默认为false,然后在更新循环中你可以检查_target是否为null。

playOnce = false // Defaulted to false
void Update()
{
if (playOnce)
{
animator.SetTrigger("Picking Up");
playOnce = false;
}
if(_target == null) return;

// Use the _target field now instead of target
Vector3 targetDirection = target.position - transform.position;
float singleStep = rotationSpeed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);
// 
playOnce = true; // Only after you have evaluated that the object is now facing towards the target.

}

由于我对3d空间了解不多,我真的不知道一个可靠的方法来知道一个对象的面向方向,但我认为它是transform.Forward。为了知道一个对象是否面对某物,你可以尝试对它的变换进行光线投射。向前或者得到两个对象变换的点积,向前。(我想这个视频可能对点积有帮助:https://www.youtube.com/watch?v=cxJnvEpwQHc)然而,如果是在2d我将愿意帮助!

最新更新