Random Teleports With Set Coorodinates

//this is the code that i am using for a 2d plat former. When i run it some times when my character jumps or moves it changes the x coordinate seemingly randomly. Do you know what is happening and can i fix it???

var zValue : float;



function Update () {

if( transform.position.z < zValue)
{
transform.position = Vector3(0,0,zValue); 

}

if( transform.position.z > zValue)
{
transform.position = Vector3(0,0,zValue); 

}

var zValue : float;

function Update () {
 
if( transform.position.z < zValue)
{
transform.position = Vector3(transform.position.x,transform.position.y,zValue); 
}
 
if( transform.position.z > zValue)
{
transform.position = Vector3(transform.position.x,transform.position.y,zValue); 
}

this will keep the existing x and y values of your character while modifying the z position.

oh… and why are you using the z axis on a 2d platformer? :smiley: