The issue here is that, when you do reach -4, you nudge it to the right and then you’re no longer <= -4 so you’re not moving right anymore. Not to mention, you’re nudging it to the left all the time, and then sometimes nudging it right as well. A nudge left followed by an equal nudge right is essentially just not moving at all. What you need is to only translate in one direction at a time, and to have that direction persist and only change when necessary. Something like this.
//Up at the top with your variables:
private Vector3 dir = Vector3.left;
//Your Update function
void Update(){
transform.Translate(dir*speed*Time.deltaTime);
if(transform.position.x <= -4){
dir = Vector3.right;
}else if(transform.position.x >= 4){
dir = Vector3.left;
}
}
The important things to note here are:
The direction persists between frames. It only changes in very specific cases
It’s generally a good idea to only do 1 translate call per frame and store your direction in a variable anyway. Explicitly setting the direction and speed is much more predictable and you always know why something moves the way it does.
I’m assuming you wanted it going from -4 to 4, although really you should store these in variables. Firstly, you can see these in the inspector, but secondly, the numbers -4 and 4 on their own don’t mean anything on their own. On the other hand, ‘leftExtent’ and ‘rightExtent’ might be -4 and 4, but are much more meaningful and, if you use these extents for other things, you don’t have to change these magic numbers everywhere.