Guys, I’m new to Unity and C#. I’m trying to make a cube move backwards until it reaches a certain point on the z position, then it then moves forwards. This is my code:
void Update () {
float zPosition = transform.position.z;
if (zPosition >= -6.5) {
transform.Translate (Vector3.back * Time.deltaTime);
} else
transform.Translate (Vector3.forward * Time.deltaTime);
}
It works perfect until -6.5 where it stops and moves backwards and forwards as the zPosition is changing between >= -6.5 and <= -6.5. I just can’t think of another way to do this. As I mentioned, I’m new to Unity and C# so I’d appreciate any input.