一旦 object1 和 object2 达到彼此之间的特定距离,我们如何才能阻止 Mathf.PingPong 速度增加?
float min;
float max;
// Update is called once per frame
void Update () {
min = object1.position.x;
max = object2.position.x;
transform.position = new Vector3(Mathf.PingPong(Time.time*2f, max-min)+min, transform.position.y, transform.position.z);
}
最佳答案
基本上,在像 Unity 这样的基于帧的系统中,“2f”是此类语句中的“速度”。
在 PingPong 中,“2”是ping 和 pong 的时间
正如 Gunnar 解释的那样,如果您担心对象的*米每秒,则必须这样做
float desiredMPS = 10f; // you want the object to move at 10 mps
float knownDistance = max - min;
float howManySecondsForLoop = knownDistance / desiredMPS;
您可以使用“howManySecondsForLoop”作为 PingPong 的“2”。
一般来说,在特定的时间或地点改变它,
public float pongTime = 2.5f // .. or whatever as above
Vector3 p = transform.position;
float newX = Mathf.PingPong(Time.time*pongTime,max-min)+min;
p.x = newX;
transform.position = p;
并尝试自己更改“pongTime”。 (只需在编辑器中执行即可。)
在代码中,您可能会使用“Invoke”或类似的方法来更改它。
Invoke( "InThreeSecondsSlowItDown", 3f);
private void InThreeSecondsSlowItDown()
{
pongTime = .75f; // or calculate as above
}
或者你可以这样做
if ( .. distance .. < .. width of enemy *2 .. )
pongTime = pongTime * .1f; // or calculate as above
享受
简而言之,试试这个
float desiredMPS;
// you want the object to move at that many meters per second
// at first try say "3" in the Editor
void Update()
{
float knownDistance = max - min;
float howManySecondsForLoop = knownDistance / desiredMPS;
public float pongTime = howManySecondsForLoop;
Vector3 p = transform.position;
float newX = Mathf.PingPong(Time.time*pongTime,max-min)+min;
p.x = newX;
}
关于c# - Mathf.PingPong 速度问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912458/