# Move in X axis and stop on walls [Solved]

I’ve started developing a Mobile game a few days ago and it’s going great.
But I’m having only 1 issue with the development of the movement system.

The player controls a racket position in its X axis using touch.
I’m using Transform.position but it ignores collisions (Walls in my case).

I tried using both Transform and Rigidbody.
I’ve got better results using Transform, but my problem hasn’t been resolved and the ball can bypass walls.

The racket built like this:
– Instantiator (The object that moves)
– Ball, the item that the player drops, Its randomly generated with different sizes.

Anybody knows how can I make Transform.position (Or another method idk) stop when colliding with walls?

Try these steps:

• make two public Transform variables, one called `LeftLimit` and one called `RightLimit`

• make two empty GameObjects (named the same) as children of your playfield above, moving them to where you want the center of the racquet to stop moving

• in the inspector, drag those two GameObjects into the variable fields

• in code just ask if the position is less or not and clamp it:

``````float newXPosition = ... however you get this now

if (newXPosition < LeftLimit.position.x)
{
newXPosition = LeftLimit.position.x;
}
if (newXPosition > RightLimit.position.x)
{
newXPosition = RightLimit.position.x;
}

// now use the new X position to drive your racquet, always within bounds now
``````

You can even shorten it a lot with Mathf.Clamp() but I prefer the longhand above because it’s more obvious and (to me) reads better.

Hey!, thanks for your awesome response
I added clamping using Mathf:

``````if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * moveSpeed, transform.position.y, transform.position.z);
transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit.position.x, rightLimit.position.x), transform.position.y, transform.position.z);
}

}
``````

The method works great, but since I’m using 4 prefabs and each one has a different size, some of them over passing the border, also by shrinking the limits sizes the small balls will be far from the border.

Do you know how I can use the same method with different ball sizes?.
I thought about storing the Limits for each prefab, but I rather prefer that it will detect walls automatically.

You could make a ball property size script to tell how big each ball is and use that value to inset the value of the clamping.

Are these balls supposed to fall into place? You may want to use 2D physics to do that part… not sure of your game design. If you did that the you would need colliders around the field.

You are hero!
I tried detecting the size of the ball using triggers but after reading your comment and researching, it works using the method you said!

This is the code:

``````    public void Update()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * moveSpeed * Time.deltaTime, transform.position.y, transform.position.z);
transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftLimit.position.x + transform.localScale.x / 2, rightLimit.position.x - transform.localScale.x / 2), transform.position.y, transform.position.z);
}

}
}
``````