# Mathf.PingPong and Velocity

Hi guys,

I need to move the fire towards to x axis and get it bouncing on y axis. I have tried the code below and it behaves weird cuz its resultant changes when i set the y position by code.

What i am trying to do is move fire towards to x axis and bounce on y axis. Can anyone help me ?

``````    void Update()
{
if (FireRight)
{
FireRb.velocity = new Vector3(transform.position.x * FireSpeed, Mathf.PingPong(Time.time, 7), 0);
}

if (!FireRight)
{
FireRb.velocity = new Vector3(-transform.position.x * FireSpeed, Mathf.PingPong(Time.time, 7), 0);
}
}
``````

Break apart those humongous calculations (line 5 and line 10) into something you can reason about. Start with a Vector3.zero assigned to a Vector3 temp variable and then add the parts you want to add and finally assign it to the .velocity component of that FireRb component.

``````var temp = Vector3.zero;
temp.y = <your mathf pingpong here>
if (blah)
{
temp.x = transform.position.x;
}
FireRb.velocity = temp;
``````

Mathf.PingPong used as you have it there will be a 14-second long loop: for 7 seconds it will get bigger (0 to 7) and then it will slowly get smaller back to zero, as one 14-second loop.

If you want it to spray up and down much faster, then assign the Time.time to a temporary variable (again so you can reason about what you’re doing) and multiply it by something like perhaps 14. In that case it will complete one full up / down cycle in 1 second.

Also, this takes the object X POSITION and uses it as its X VELOCITY… that seems unlikely to be correct.

@Kurt-Dekker thanks for reply. I don’t think that would work as it will set its X positions to Zero in Update method. So the fire object is gonna stop and won’t bounce. I might be missing something so I will try that method when i get to home.

I got it worked after thinking and searching. I leave it on here so it’d help someone else.

Make sure that you turn off gravity, angular and linear drags so it will keep moving by the force at start.

``````    void Start()
{
if (playerMoveCs.facingRight)
{
FireRb.velocity = new Vector3(1, 0, 0) * FireSpeed;
}

if (!playerMoveCs.facingRight)
{
FireRb.velocity = new Vector3(-1, 0, 0) * FireSpeed;
}
}
void FixedUpdate()
{
transform.position = new Vector3(transform.position.x, Mathf.Lerp(transform.position.y + 1, transform.position.y - 1, Mathf.PingPong(Time.time, 1)), 0);
}
``````