Mathf.PingPong速度问题?

时间:2016-03-10 09:31:44

标签: c# unity3d

一旦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);
}

1 个答案:

答案 0 :(得分:3)

基本上,“2f”是Unity中基于帧的系统中“速度”

在PingPong中,“2”是 ping和乒乓的时间

正如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( "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;
 }