i use this code to rotate my avatar to targetPostion, but the problem is it alway rotate too soon & faster then the duration.
我使用这个代码将我的化身旋转到Target Postion,但问题是它总是旋转得太快&比持续时间快。
any ideas?
有什么好主意吗?
IEnumerator MoveToPosition(GameObject avatar, Vector3 targetPosition, float speed)
{
// Rotate towards the target
Vector3 direction = targetPosition - avatar.transform.position;
Quaternion toRotation = Quaternion.LookRotation(direction);
float STARmove = 0;
float duration = 1;
while (STARmove < duration)
{
STARmove += Time.deltaTime;
// Calculate rotation based on interpolation ratio
float t = Mathf.Clamp01(STARmove / duration);
avatar.transform.rotation = Quaternion.Lerp(avatar.transform.rotation, toRotation, t);
// Wait for a fixed time before next update
yield return new WaitForSeconds(Time.deltaTime);
}}
更多回答
优秀答案推荐
Interpolate between the start and end rotations, not between the current and end rotations.
在开始旋转和结束旋转之间进行插补,而不是在当前旋转和结束旋转之间进行插补。
更多回答
我是一名优秀的程序员,十分优秀!