# Fix Move Object Code

I have this file that can Move an Object on X, Y, Z. My only problem is if I try and use negative variables I dont see anything happening in the game. Anyone able to help me fix this? Here is what I used below:

using UnityEngine;

public class SmoothMover : MonoBehaviour
{
public float speedX = 0f;
float startX;
public float distanceX = 0;
public float speedY = 0f;
float startY;
public float distanceY = 0;
public float speedZ = 0;
float startZ;
public float distanceZ = 0;

``````void Start()
{
startX = transform.position.x;
startY = transform.position.y;
startZ = transform.position.z;
}

void Update()
{
addX = Mathf.PingPong(Time.time * speedX, distanceX);
transform.position = new Vector3(startX + addX, transform.position.y, transform.position.z);
addY = Mathf.PingPong(Time.time * speedY, distanceY);
transform.position = new Vector3(transform.position.x, startY + addY, transform.position.z);
addZ = Mathf.PingPong(Time.time * speedZ, distanceZ);
transform.position = new Vector3(transform.position.x, transform.position.y, startZ + addZ);
}
``````

}

The problem is with your use of PingPong. If you add the following script to a cube, it will move smoothly between 0 and 3:

``````using UnityEngine;

public class Test1 : MonoBehaviour
{
private void Update()
{
float x = Mathf.PingPong(Time.time, 3);
transform.position = new(x, 0, 0);
}
}
``````

However, when you specify -3 instead of 3, the cube jumps immediately to (-3, 0, 0) and stays there. A common function is that people want to oscillate between, say, -3 and 3. in that case, you would use:

``````using UnityEngine;

public class Test1 : MonoBehaviour
{
private void Update()
{
float x = Mathf.PingPong(Time.time, 6);
transform.position = new(x - 3, 0, 0);
}
}
``````

In your case, it depends on what it is you are trying to do. If itâ€™s a variant of -3 to 3, just adapt my second example. In case you are interested, here is the actual code that Unity devs wrote for PingPong:

``````        // PingPongs the value t, so that it is never larger than length and never smaller than 0.
public static float PingPong(float t, float length)
{
t = Repeat(t, length * 2F);
return length - Mathf.Abs(t - length);
}
``````