C# 如何旋转并将对象移动到另一个对象位置和旋转?

C# 如何旋转并将对象移动到另一个对象位置和旋转?,c#,unity3d,rotation,C#,Unity3d,Rotation,首先是一些截图 第一个是我要移动到的对象,并旋转到它的旋转: 这是目标:您可以看到它的位置和旋转: 此屏幕截图是我想要旋转和移动的对象,因此它将定位在与目标相同的位置和旋转: 它们看起来像相同的旋转和位置,但我想要旋转和移动的对象有Animator,当游戏开始时,我播放旋转和移动对象的动画,当动画结束时,我想要旋转移动对象,使其像目标位置和旋转 此脚本附加到要旋转和移动的对象: using System.Collections; using System.Collections.Generi

首先是一些截图

第一个是我要移动到的对象,并旋转到它的旋转: 这是目标:您可以看到它的位置和旋转:

此屏幕截图是我想要旋转和移动的对象,因此它将定位在与目标相同的位置和旋转:

它们看起来像相同的旋转和位置,但我想要旋转和移动的对象有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);
    }
}
这很好用。但是它在更新中(删除了刚体,所以可以在更新中而不是FixedUpdate中)。当协同程序完成后,如何在等待几秒钟后使其在IEnumerator内工作

我现在在IEnumerator中就是这样做的:

问题是它永远不会结束,而它却在中间不断循环。 它不是冻结编辑器或其他东西,但它永远不会完成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;
    }

为什么必须使用
更新
?你不能简单地继续使用协同程序而不是让
Update
poll检查bool标志吗?你试过了吗?另请注意:如果你正在移动/旋转的物体是一个
刚体
,你不应该在
更新
中使用
变换
,而是
刚体。移动位置
刚体。移动旋转
固定更新
中,你也在告诉它朝着目标对象旋转。。。你没有告诉它复制目标旋转…@derHugo我如何在continue中实现它?当“继续”结束时,我是否需要使用while循环进行旋转和移动?例如是。。但别忘了
返回null在IT中为什么必须使用
Update
?你不能简单地继续使用协同程序而不是让
Update
poll检查bool标志吗?你试过了吗?另请注意:如果你正在移动/旋转的物体是一个
刚体
,你不应该在
更新
中使用
变换
,而是
刚体。移动位置
刚体。移动旋转
固定更新
中,你也在告诉它朝着目标对象旋转。。。你没有告诉它复制目标旋转…@derHugo我如何在continue中实现它?当“继续”结束时,我是否需要使用while循环进行旋转和移动?例如是。。但别忘了
返回null在其中
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;
    }