1

How can we stop Mathf.PingPong speed from increasing once the object1 and object2 have reached a specific distance between each other?

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);
}
d12frosted
  • 1,280
  • 12
  • 33

1 Answers1

3

basically the "2f" is the "speed" in such statements in a frame-based system like Unity.

here in PingPong, the "2" is the time of the ping and the pong

As Gunnar explains if you are concerned with the *meters per second of the object, you have to do this

float desiredMPS = 10f; // you want the object to move at 10 mps
float knownDistance = max - min;
float howManySecondsForLoop = knownDistance / desiredMPS;

You would use "howManySecondsForLoop" as the "2" for the PingPong.


in general, to change that at certain times or places,

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;

and experiment with changing the "pongTime" yourself. (Just do it in the editor.)

In code it is likely you will use "Invoke" or similar to change it.

Invoke( "InThreeSecondsSlowItDown", 3f);

private void InThreeSecondsSlowItDown()
 {
 pongTime = .75f; // or calculate as above
 }

Or you might do something like this

if ( .. distance .. < .. width of enemy *2 .. )
  pongTime = pongTime * .1f; // or calculate as above

Enjoy

in short try this

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;
 }
Ian H.
  • 3,840
  • 4
  • 30
  • 60
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • As edition: The speed is actually not changing in the original code (the 2f, which should be replaced with the speed variable), it only seems like it does because the distance is larger. So what should probably be done is "normalizing" the speed after the desired distance so it seems constant. If for example one way from A to B is travelled in 2 seconds with a distance of 5 units, it would be travelled in 4 seconds with a distance of 10 units for it to look the same speed. – Gunnar B. Mar 10 '16 at 13:49
  • provide implementation example please boss? –  Mar 10 '16 at 18:19
  • thank you friend, i will see it in moments or tomorrow and give you good reply or not. –  Mar 10 '16 at 18:30
  • This code does not make the speed stop increasing at a certain distance between max and min! For example distance = 6 –  Mar 10 '16 at 18:43
  • @JoeBlow Did you read my latest comment? Please see your script again. –  Mar 11 '16 at 16:35
  • i can assure that the ***speed*** (meters per second) is constant with the above. (1) it could be you want the ***time*** to change in some way (perhaps become a constant. if so do that (2) if you want to change (anything) "above 6", use an `if` statement, it would be something like `if ( d > 6f )` – Fattie Mar 11 '16 at 16:59
  • @VietLau If this fixed your problem, just accept it as the answer. – Programmer Mar 11 '16 at 21:28
  • looking forward to your next question @VietLau – Fattie Mar 14 '16 at 18:43