c# - 如何旋转并将对象移动到另一个对象位置和旋转?
问题描述
先来几张截图。
第一个是我想要移动并旋转到它的旋转对象:这是目标:你可以看到它的位置和旋转:
此屏幕截图是我要旋转和移动的对象,因此它将定位在与目标相同的位置和旋转:
它们看起来像相同的旋转和位置,但我想旋转和移动的对象有 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);
}
}
我正在使用结束标志来告诉动画何时结束播放,然后在更新中开始旋转(我还需要进行移动)。但它并没有腐烂得太光滑,也没有移动。
我希望它像从动画剪辑结束时的过渡一样平滑地旋转和移动,直到它与目标的位置和旋转相同。
但最后现在只旋转到最后它是这样的,而不是像目标:
我正在使用 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内循环。它不会冻结编辑器或其他东西,但它永远不会完成 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;
}
解决方案
推荐阅读
- powershell - Powershell:无法隐藏“删除项目”上的“访问被拒绝”错误
- php - 尝试查看电子邮件是否不重复
- r - 在向量中重新编码整数,以便它们注册为 NA ?
- python - 拉伸图像以适应 kivy 中的网格布局
- javascript - 该站点如何形成 POST 请求的标头?
- c# - 我需要增加和减少 button.width 使用 ScrollBar 更改(C# Win Form)
- php - 如何从 cURL POST 保存连接 IP
- r - 如何修复 plm 包中的“重复的 row.names”?
- c++ - 如何在重写函数中返回模板派生类而不是基类?
- c# - 检查开始时间和结束时间之间的时间差应该是1小时30分钟,如果超过它不应该继续