我有一个向玩家移动的弹丸,但是我希望能够根据其移动的距离来调整其速度。
转换变量为:
弹丸始于firePoint,并在到达playerTransform时被摧毁
speedMultiplier的初始值为1,但应根据播放器的距离更改为0.5到1.5之间的值。
// This is called every time a projectile is fired
float speedMultiplier = 1f;
speedMultiplier = FORMULA TO CALCULATE HOW MUCH THE SPEED SHOULD CHANGE; // Should be between 0.5 and 1.5
projectileSpeed *= speedMultiplier;
答案 0 :(得分:1)
您可以使用Mathf.lerp(a, b, t)
。 lerp方法基于第三个参数在两个值之间线性内插。将最后一个参数想成百分比。
例如:
Math.lerp(0.0f,10.0f,0.0f); //Will yield 0 because 0 is 0% between 0 and 10
Math.lerp(0.0f,10.0f,0.5f); //Will yield 5 because 5 is 50% between 0 and 10
Math.lerp(0.0f,10.0f,1.0f); //Will yield 10 because 10 is 100% between 0 and 10
因此,在您的示例中,您可以根据x位置进行调整,但需要将其缩放到0到1之间(0表示x = -7.5,1表示x = 4)。
float t = Mathf.clamp01((transform.position.x + 7.5) / (4 + 7.5));
方法Mathf.clamp01(x)
仅将数字限制在0或1之间。
现在只是在速度之间徘徊:
Mathf.lerp(0.5f, 1.5f, t);