# How To Limit A transform MOVEMENT in X axis

I am currently working on a game where Camera movement is around X axis only like angory birds. I am applying transform.translate to move the camera.

My problem is that

minoffset //minimum value of camera x position
maxoffset //maximum value of camera x postion
I using following code

`if (transform.position.x >= MinOffset && transform.position.x <= MaxOffset)
{

do the movement stuff

}`

when I use transform. translate using touch The position of camera moves outside the offset values .and the code written in if satement never executes.

I have also tried with two another conditions to check is x position goes beyond the offset value. Reset its position to maximum value. But It produces a jerk .Please Help

Maybe clamp would be more elegant to make limits
transform.position = Vector3(Mathf.Clamp(pObject.x, min, max), pObject.y, pObject.z);

I think that it jerks because you translate it, check if its out of limits, and if it is out of limits, you move it back to within the limits the next frame.

Better way to do it, is to check the final position, and if that is within your boundaries, then do the translate. Something like this pseudo code:

``````private Vector3 translateVector = new Vector3(10.0f, 0.0f, 0.0f);

update()
{
Vector3 newPosition = PlayerObject.Transform.Position + translateVector;

if(newPosition.x >= MinOffset && newPosition.x <= MaxOffset)
{
PlayerObject.Transform.Translate(translateVector);
}
}
``````

Good luck!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour {
public Transform cube;
public float dist2;
Vector3 initPos;
// Use this for initialization
void Start () {
initPos = transform.position;
dist2 = cube.lossyScale.x / 2;
}

``````// Update is called once per frame
void Update () {
float h = Input.GetAxis ("Horizontal");
float s = 0;
if (h != 0) {
s = h > 0 ? 1 : -1;
}
Vector3 newPos = transform.position + cube.right * s;

if(Mathf.Abs (newPos.x) <= dist2)
transform.Translate (cube.right *s * 0.08f);
}
``````

}