Transform.Translate problem within if and while

Hi there dear experts,

I currently work on an ingame code field function which should give the player a visual feedback when pressing a key.

1261914--55446--$pressbutton.png

If the player presses the button I want to make it move slighly back and forth on the Z axis to give it a realistic “I pressed that button” feeling.
For this, I use an if clause including a while loop in which the object should be moved back and forth. However, this does not work. Plus, I do not want to use an animation for this.

This is the code:

function Pressed()
{
	// Activate Feedback Movement
	phase = 1;		
}

function Update () {

	// Feedback
 	if (phase == 1)
 	{
 	print ("1"); // THIS PRINT WORKS
 	
// MOVE OBJECT BACK
 	while(this.transform.position.z < 1)
 	{
 		transform.Translate(Vector3(0,0,0.1) * 1 * Time.deltaTime);
 	}	 			
 		 		
 	phase = 2;
 	
 	}
 		
 	if (phase == 2)
 	{
 		print ("2"); // THIS PRINT WORKS AS WELL
 	
// MOVE OBJECT FORTH
	 	while(this.transform.position.z > 0)
	 	{
	 		transform.Translate(Vector3(0,0,-0.1) * 1 * (Time.deltaTime * 0.01));
	 	}	 

 		phase = 0;			
 		this.transform.position.z = 0;
 	}

}

It also does not work with localPosition.
I guess I just forgot some condition or reasonably easy solution for this or am just thinking too complicated about it.

I would be pleased to get some replies. Thank you.

Aries

Your while loops won’t have the desired effect. In this case both of your while loops will complete during a single update.
Your logic works like this.
Frame 1: Update starts… Do first loop condition till complete. Set phase 2. Do second loop condition till complete. Update ends. Camera displays
Frame 2: Update starts… Do first loop condition till complete. Set phase 2. Do second loop condition till complete. Update ends. Camera displays

You likely want to use a co-routine (called once when pressed) or a separate function (called repeatedly from update till movement is complete). This function will then move the button. Your while loops will turn into if conditions. Each frame offset the position. Test to see if has reached bottom or complete. If bottom reverse move direction. If done do no more.

///in update
if(phase == 1){
phase = 0; // prevent reenter.
performWhile();
}

///in performWhile()
while(somethingTrue){
}

Thank you very much. Your tip helped me a lot and it works now.
To conclude this topic and help upcoming generations, this is the working code:

function Pressed()
{
	// Activate Feedback Movement
	phase = 1;		
}

function Update () {

	// Feedback
 	if (phase == 1)
 	{
 		FeedbackBack();					 	 	
 	}
 		
 	if (phase == 2)
 	{
 		FeedbackForth();	 
 	}
}

function FeedbackBack()
{
 	if(this.transform.localPosition.z < 0.24)
 	{
 		transform.Translate(Vector3(0,0,0.1) * Time.deltaTime);
 	}
 	else
 	{
		phase = 2;  	
 	}
}

function FeedbackForth()
{
 	if(this.transform.localPosition.z > 0)
 	{
 		transform.Translate(Vector3(0,0,-0.1) * Time.deltaTime);
 	}
 	else
 	{ 		
	 	phase = 0;
	 	this.transform.position.z = 0;		
 	}
}