C# 如何以任何所需速度将角色从一个点移动到特定点?
我使用了此代码,但它不这样做:C# 如何以任何所需速度将角色从一个点移动到特定点?,c#,unity3d,C#,Unity3d,我使用了此代码,但它不这样做: public Transform[] points; public float speed; private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "door1") { transform.position = Vector3.Lerp(transform.position, points[1].position,
public Transform[] points;
public float speed;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "door1")
{
transform.position = Vector3.Lerp(transform.position, points[1].position, speed * Time.deltaTime);
}
}
也就是说,我希望当猪触动扳机时,它能以期望的速度飞到地面上的一个更高的点。请看附件中的照片
OnTriggerEnter2D在每次碰撞器输入时仅调用一次。如果您仅以速度*Time.deltaTime和Time.deltaTime移动它,则deltaTime在0.008-0.100之间,它可能只会稍微移动
根据需要,是否确定不完全移动对象,或者在更新方法中设置开始移动对象的标志?这里有两个问题:
当您进入碰撞器时,您只调用该行一次,因此运动应用于单个帧!
Lerp使用介于0和1之间的因子在两个位置之间插值线性。你每次都使用当前位置作为起始点,所以如果你连续地调用它,会发生什么事情,是近似的位置,每一帧都变得越来越慢,这不是你所描述的。你想以恒定的速度移动。
您很可能更愿意使用and
或者,看起来更复杂但功能更强大的方法是根据速度计算所需的时间,但仍然添加一些平滑,例如
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("door1"))
{
StartCoroutine (MoveTo(points[1].position, speed);
}
}
private IEnumerator MoveTo(Vector3 targetPosition, float averageSpeed)
{
// store the initial position
var from = transform.position;
// Get the expected duration depending on distance and speed
var duration = Vector3.Distance(from, targetPosition) / averageSpeed;
// This is increased over time
var timePassed = 0;
while(timePassed < duration)
{
// This linear grows from 0 to 1
var factor = timePassed / duration;
// Adds some ease-in and ease-out at beginning and end of the movement
factor = Mathf.SmoothStep(0, 1, factor);
// linear interpolate on the smoothed factor
transform.position = Vector3.Lerp(from, targetPosition, factor);
// increase by time passed since last frame
timePassed += Time.deltaTime;
// Tell Unity to "pause" the routine here, render this frame
// and continue from here in the next frame
yield return null;
}
// to be sure to end up with exact values set the target position fix when done
transform.position = targetPosition;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("door1"))
{
StartCoroutine (MoveTo(points[1].position, speed);
}
}
private IEnumerator MoveTo(Vector3 targetPosition, float averageSpeed)
{
// store the initial position
var from = transform.position;
// Get the expected duration depending on distance and speed
var duration = Vector3.Distance(from, targetPosition) / averageSpeed;
// This is increased over time
var timePassed = 0;
while(timePassed < duration)
{
// This linear grows from 0 to 1
var factor = timePassed / duration;
// Adds some ease-in and ease-out at beginning and end of the movement
factor = Mathf.SmoothStep(0, 1, factor);
// linear interpolate on the smoothed factor
transform.position = Vector3.Lerp(from, targetPosition, factor);
// increase by time passed since last frame
timePassed += Time.deltaTime;
// Tell Unity to "pause" the routine here, render this frame
// and continue from here in the next frame
yield return null;
}
// to be sure to end up with exact values set the target position fix when done
transform.position = targetPosition;
}