Can't get the code that should rotate a player and change the speed to work

Hello everyone I have 2 rigidbodys with scripts attached. I made the first script to change the rotation of an object1 and the speed to negative(just because the controls would not go reverse) when the object1 X coordinates are bigger than object2 X coordinates. The problem is that when the object1 coordinates gets bigger than the object2, object1 gets stuck in that position and goes bugy. I don’t understand why does that happen and I would really appreciate your help. Here is the video of my problem https://www.youtube.com/watch?v=1yjfxz1W17I, I find it difficult to explain this by writing :slight_smile:

The object1(player1) move script:

var object1 : Transform;
var object2 : Transform;
 
var Xspeed = 10.0;
var Yspeed = 10.0;
 
function Start() {
 
}
 
function FixedUpdate () {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * Xspeed;
var y = Input.GetAxis("Vertical") * Time.deltaTime * Yspeed;
transform.Translate(x, y, 0);
 
if(object1.transform.position.x > object2.transform.position.x){
    transform.Rotate(new Vector3(0,180,0));
    Xspeed = -10.0;
    print("Rotating");
}
else{
    transform.Rotate(new Vector3(0,0,0));
    Xspeed = 10.0;
    print("RotatingBack");
}
}

The object2(player2) move script:

#pragma strict
 
var speed = 10.0;
 
function FixedUpdate () {
var x = Input.GetAxis("Horizontal2") * Time.deltaTime * speed;
var y = Input.GetAxis("Vertical2") * Time.deltaTime * speed;
transform.Translate(x, y, 0);
}

It may have something to do with rotating it 180 degrees and back to 0, do you really need to do that? Do you just want player one to not go past player2? You could just try something like if (object1.position.x > object2.position.x) { object1.position.x = object2.position.x } or to keep a little further away… if (object1.position.x > object2.position.x-1) { object1.position.x = object2.position.x-1 }