Aqu*_*Dev 0 c# unity-game-engine
所以我想让玩家去某个地方,但我不希望它看起来像是传送,所以我使用了Vector3.Slerp,但问题是它不会将目标停在中途停止.
这是代码:
void Update()
{
if (Input.GetKeyDown("space"))
{
StartCoroutine(GoToTargetPos());
}
} IEnumerator GoToTargetPos()
{
Vector3 targetPos = transform.position + offset;
transform.position = Vector3.Slerp(transform.position, targetPos, Time.deltaTime * speed);
yield return new WaitForSeconds(0.75f);
isDone = true;
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果我的英语不好.
快速回答,只是为了表明它正在工作......
IEnumerator GoToTargetPos()
{
Vector3 targetPos = transform.position + offset;
float timeElapsed = 0.0f;
float timeToMove = 0.75f; // Using this since that is how you are waiting...
while(timeElapsed < timeToMove)
{
timeElapsed += Time.deltaTime;
float ratio = Mathf.Clamp01(timeElapsed / timeToMove);
transform.position = Vector3.Slerp(transform.position, targetPos, ratio);
yield return null; // Return to this at the beginning of the next frame
}
isDone = true;
}
Run Code Online (Sandbox Code Playgroud)