unity弹跳球同高下落速度控制
Unity bouncing ball with the same height and falling speed control
如何在 unity 中创建一个弹跳球,它会弹跳到相同的高度,并且可以让它下落得更快或更慢?一个人告诉我使用:
heightVector * |sin(time * speed)|
但我不知道在哪里插入它。我对这些东西真的一窍不通。求助!
你的公式是正确的。
heightVector
是你的球的最大高度。例如如果是(0,10)
,则表示你的球会飞到10米高。
time
只是一个计时器。
speed
是球的速度。
但是,我建议将速度乘以 Time.deltaTime
以使反弹帧速率独立。
让我们开始编写代码。
heightVector
和 speed
没有复杂性。只需创建两个 public 字段即可!
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector2 HeightVector = new Vector2(0,10);
}
要创建计时器,您需要一个 float
变量。然后你需要在每次 Update
调用时添加 Time.deltaTime
。
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector2 HeightVector = new Vector2(0,10);
float timer;
void Update()
{
timer += Time.deltaTime;
}
}
恭喜!你现在有了你的计时器!
现在你真的接近尾声了。你只需要计算球的当前位置并将其应用于它的变换。
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector3 HeightVector = new Vector3(0,10);
float timer;
void Update()
{
timer += Time.deltaTime;
Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
transform.position = currentPosition;
}
}
现在您需要将 Bouncer 脚本附加到您的球上,您的球应该开始弹跳了!
编辑:
如果您希望球保持其原始位置并从那里反弹,您需要保持原始位置并将计算出的位置附加到它:
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector3 HeightVector = new Vector3(0,10);
Vector3 originalPosition;
float timer;
void Start()
{
originalPosition = transform.position;
}
void Update()
{
timer += Time.deltaTime;
Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
transform.position = originalPosition + currentPosition;
}
}
如何在 unity 中创建一个弹跳球,它会弹跳到相同的高度,并且可以让它下落得更快或更慢?一个人告诉我使用:
heightVector * |sin(time * speed)|
但我不知道在哪里插入它。我对这些东西真的一窍不通。求助!
你的公式是正确的。
heightVector
是你的球的最大高度。例如如果是(0,10)
,则表示你的球会飞到10米高。
time
只是一个计时器。
speed
是球的速度。
但是,我建议将速度乘以 Time.deltaTime
以使反弹帧速率独立。
让我们开始编写代码。
heightVector
和 speed
没有复杂性。只需创建两个 public 字段即可!
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector2 HeightVector = new Vector2(0,10);
}
要创建计时器,您需要一个 float
变量。然后你需要在每次 Update
调用时添加 Time.deltaTime
。
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector2 HeightVector = new Vector2(0,10);
float timer;
void Update()
{
timer += Time.deltaTime;
}
}
恭喜!你现在有了你的计时器!
现在你真的接近尾声了。你只需要计算球的当前位置并将其应用于它的变换。
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector3 HeightVector = new Vector3(0,10);
float timer;
void Update()
{
timer += Time.deltaTime;
Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
transform.position = currentPosition;
}
}
现在您需要将 Bouncer 脚本附加到您的球上,您的球应该开始弹跳了!
编辑: 如果您希望球保持其原始位置并从那里反弹,您需要保持原始位置并将计算出的位置附加到它:
class Bouncer : MonoBehaviour
{
public float Speed = 10;
public Vector3 HeightVector = new Vector3(0,10);
Vector3 originalPosition;
float timer;
void Start()
{
originalPosition = transform.position;
}
void Update()
{
timer += Time.deltaTime;
Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
transform.position = originalPosition + currentPosition;
}
}