- Home /
How can i Limit the movement of character (x, y axis) on camera view
I want a controller like this one:
this is what i actually have:
what I want is to create a limit for x and y axis (as you can see the caracter goes out of the screen view)
Ive tryed with clamp, also making a limit for the mouse movement.. but nothing works.
this is my script:
public Vector3 targetPos;
public float speed = 2.0f;
// Use this for initialization
void Start () {
targetPos = transform.position;
}
// Update is called once per frame
void Update () {
float distance = transform.position.z - Camera.main.transform.position.z;
targetPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance-5);
targetPos = Camera.main.ScreenToWorldPoint (targetPos);
Vector3 followXonly = new Vector3 (targetPos.x, targetPos.y, transform.position.z);
transform.position = Vector3.Lerp (followXonly, followXonly, speed * Time.deltaTime);
}
i think that the lerp is wrong, but actually is giving what i want.
thanks for your help.
Comment
Your answer
Follow this Question
Related Questions
Restrict camera rotation in x and y? 1 Answer
Making a Paddle Move Vertically? 1 Answer
How to stop a Gameobject moving on the X axis 1 Answer
Calculating the Angle between two vectors 2 Answers
2d movement 1 Answer