首先是一些屏幕截图。
第一个是我要移动并旋转的对象: 这是目标:您可以看到它的位置和旋转:
此屏幕快照是我要旋转和移动的对象的快照,因此它将与目标一样位于相同的位置和旋转:
它们看起来像是相同的旋转和位置,但是我要旋转和移动的对象具有Animator,当游戏开始时,我播放动画,当动画结束播放时,我旋转并移动对象,我想将其旋转为如目标位置和旋转。
此脚本已附加到我要旋转和移动的对象上:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayAnimation : MonoBehaviour
{
// The target marker.
public Transform target;
// Angular speed in radians per sec.
public float speed = 1.0f;
private Animator anim;
private bool started = true;
private float animationLenth;
private bool ended = false;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
private void Update()
{
if (Whilefun.FPEKit.FPESaveLoadManager.gameStarted == true && started == true)
{
Whilefun.FPEKit.FPEInteractionManagerScript.Instance.BeginCutscene();
anim.enabled = true;
anim.Play("Stand Up", 0, 0);
animationLenth = anim.GetCurrentAnimatorStateInfo(0).length;
StartCoroutine(AnimationEnded());
started = false;
}
if (ended == true)
{
// Determine which direction to rotate towards
Vector3 targetDirection = transform.position - target.position;
// The step size is equal to speed times frame time.
float singleStep = speed * Time.deltaTime;
// Rotate the forward vector towards the target direction by one step
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
// Calculate a rotation a step closer to the target and applies rotation to this object
transform.rotation = Quaternion.LookRotation(newDirection);
}
}
IEnumerator AnimationEnded()
{
yield return new WaitForSeconds(animationLenth);
ended = true;
Whilefun.FPEKit.FPEInteractionManagerScript.Instance.EndCutscene();
anim.enabled = false;
//transform.gameObject.SetActive(false);
}
}
我正在使用end标志来指示动画何时结束播放,然后在Update中开始旋转(我也需要移动)。但是它并没有腐烂得太滑并且还没有移动。
我希望它可以旋转并平滑移动,就像从动画剪辑结束到它将位于与目标相同的位置和旋转一样过渡。
但是最后,直到最后才旋转,就像这样,而不像目标:
我正在使用RotateTowards和MoveTowards
if (ended == true)
{
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Rotate our transform a step closer to the target's.
transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
headTransform.rotation = Quaternion.RotateTowards(headTransform.rotation, target.rotation, step);
}
这很好。但这在更新中(已删除刚体,因此可以在更新中而不是在固定更新中使用)。协程完成后,如何在WaitForSeconds之后在IEnumerator中工作?
这就是我现在在IEnumerator中进行操作的方式:
问题在于,它永远不会结束,而它会在while内不断循环。 它并没有冻结编辑器之类的东西,但是它永远不会完成while循环。
IEnumerator AnimationEnded()
{
yield return new WaitForSeconds(animationLenth);
Vector3 currentPosition = transform.position;
while (Vector3.Distance(currentPosition, target.position) > 0.1f)
{
var step = speed * Time.deltaTime;
// Rotate our transform a step closer to the target's.
transform.rotation = Quaternion.RotateTowards(transform.rotation, target.rotation, step);
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
headTransform.rotation = Quaternion.RotateTowards(headTransform.rotation, target.rotation, step);
yield return null;
}
Whilefun.FPEKit.FPEInteractionManagerScript.Instance.EndCutscene();
anim.enabled = false;
}