I translate my object forward,i m using for loop.

I translate my object forward,i m using for loop.but object move forward continuosly, it not stop.can u give suggestion.

using UnityEngine;
using System.Collections;
[AddComponentMenu(“EZ GUI/Controls/Button”)]

public class Translate : MonoBehaviour {
	Vector3 newpos;
	int i;
	// Use this for initialization
	void Start () {
	


}
	
	// Update is called once per frame
	void Update () {
	
		
		movement();
			
	}


 public void movement()
    	{
    		for(i = 0;i <= 2 ;i++)
    		{
    			

newpos = transform.position;
        			this.newpos.x += 0.05f;
    			

transform.position= newpos;
        			
    		}
    	}
    }

Your calling movement() every update, move that to a key press or something.

update() {

 if ( bool )
    movement()

}

Something like that.

Rakesh12,

You’re script is calling the “movement” function every frame, which means it will always be performing that function and, as that function makes the object move forward, it will never stop.

You want to give it a condition under which to move.
For example, if I am pressing the Up arrow, I would expect to move forward, but if I am not pressing it, it should not move.

If (Input.GetKey("up"))
{
  movement();
}

Hi,

If I understand you correctly, you want it to move forward but stop at a point, is that correct?

I can help you there:

using UnityEngine; 
using System.Collections; 
    
public class Translate : MonoBehaviour {

private float newposX;

void Update () {

for (int i = 0; i <= 2 ; i++) {

   newposX += 10f;

}

   transform.position = new Vector3(newposX * Time.deltaTime, transform.position.y, transform.position.z);

}


}

Iv’e removed the need to have a separate move function and the need for a separate int variable. You’re object will only move on the x axis by 20 units over time and stay there forEVER…

Which is ok I guess.

Cheers!

Mithos