# How do I change the transform position of my player when it reaches a certain x-position?

I want to make my player’s x-position change to a different one when it hits a certain x-position. For example, when my player’s x-position is less than -10.0f, I want it to become 10.0f. This is my script so far, but it’s not working:

``````	void Update () {

if (transform.position.x < -10.0ff) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
}
if (transform.position.x > 10.0ff) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
}
``````

Unity should be giving you an error about “unexpected symbol ‘f’” because of lines 4 and 7 in your sample code. Otherwise, it should work, although it would be better to have if/else if instead of two separate if branches since they are exclusive.

``````void Update () {
if (transform.position.x < -10.0f) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
} else if (transform.position.x > 10.0f) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
}
``````

It’s not an error but you probably want to add/subtract 20.0f to/from position.x instead of just setting x to min/max. Consider if position.x is -10.5f. When you wrap around, you generally want to retain the remainder beyond -10.0f (-0.5f) so your new position would be 9.5f instead of 10.0f. Otherwise your object may appear to stall when it wraps around, moving only a fraction of the distance that was intended.

``````void Update () {
if (transform.position.x < -10.0f) {
transform.position = new Vector3 (transform.position.x + 20.0f, transform.position.y, transform.position.z);
} else if (transform.position.x > 10.0f) {
transform.position = new Vector3 (transform.position.x - 20.0f, transform.position.y, transform.position.z);
}
}
``````

Hello, @S_jay1
Replace :

``````if (transform.position.x < -10.0ff) {
transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
}
if (transform.position.x > 10.0ff) {
transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
}
``````

With :

``````transform.position = new Vector3(Mathf.Clamp(transform.position.x, -10f, 10f), transform.position.y, transform.position.z);
``````

## What Mathf.Clamp does?

Mathf.Clamp will check if a value is less than the minimum(-10f here) and return the minimum if its lower, and check if a value is higher tha the maximum(10f here) and return the maximum if its higher.