Hi
I want to move an object (wall) automatically in the axis Z , (n =value)
I use the function "transform.Translate( Time.deltaTime*speed ,0, 0); but How I stop the object when I move or translate (n)
Thanks in advance
you stop calling the translate function, you can save the position where you started, and compare the z distance to the current z distance and stop when its n or bigger
private Vector3 startPosition;
private float wantedDistance;
private float speed;
void Start(){
startPosition = transform.position;
}
void Update(){
if(Mathf.Abs(startPosition.z - transform.position.z) > wantedDistance){
transform.Translate(0, 0, speed*Time.deltaTime);
}
}
The problem with this code however, is that it checks every frame for a movement that may take only a couple of seconds. You may want to disable the script when it is done, or run this in a coroutine. I wanted to have the code as simple as possible, as an example.