- Home /
How to set boundaries with camera movement
Hello,
This is my camera movement :
void Update () {
var movement = Vector3.zero;
if (Input.GetKey("a"))
movement.x--;
if (Input.GetKey("d"))
movement.x++;
transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}
I know I can limit it by using Matf.Clamp, but I don't understand how to implement it. Tried different implementation and all without success.
Any help please ?
Thanks
No problem! I've converted it to an answer. Let me know if you have any additional questions.
Answer by hiddenspring81 · Jun 01, 2013 at 10:09 PM
Have you tried
transform.Translate(movement * speed * Time.deltaTime, Space.Self);
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, MIN_X, MAX_X),
Mathf.Clamp(transform.position.y, MIN_Y, MAX_Y),
Mathf.Clamp(transform.position.z, MIN_Z, MAX_Z));
Where MIN_X
, MAX_X
, MIN_Y
, MAX_Y
, MIN_Z
, and MAX_Z
are your min and max values, in world space.
Answer by Cowboykilla · Jun 01, 2013 at 10:49 PM
maybe try something like this:
var cam : GameObject;
void Start() {
cam = GameObject.Find("Main Camera");
}
void Update() {
var movement = Vector3.zero;
if ((Input.GetKey("a")) && (cam.transform.x > -10) && (cam.transform.x < 10))
{
movement.x--;
}
if ((Input.GetKey("d")) && (cam.transform.x > -10) && (cam.transform.x < 10))
{
movement.x++;
}
transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}
Hope this helps.
Answer by 3dmind · Jun 16, 2013 at 01:02 PM
Hello, i'v tried the script above`var cam : GameObject;
void Start() {
cam = GameObject.Find("Main Camera");
}
void Update() { var movement = Vector3.zero;
if ((Input.GetKey("a")) && (cam.transform.x > -10) && (cam.transform.x < 10))
{
movement.x--;
}
if ((Input.GetKey("d")) && (cam.transform.x > -10) && (cam.transform.x < 10))
{
movement.x++;
}
transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}` and it work as i need but the problem is when a limit is reached the script stops and camera doesn't move again to the opposite direction
Answer by SheZii · Feb 07, 2019 at 08:04 AM
If you want to clamp/restrict gameobjects on the boundary of Minimap, here are the links
Youtube: https://www.youtube.com/watch?v=xW4zZQhRKlI
Step by step tutorial: http://techscenarios.com/unity3d-clamp-objects-on-edges-of-minimap-clamp-on-minimap-boundary-free-scripts-part-4/
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Jerky Camera and Varying Translate speeds 1 Answer
Mech Movement/Camera Help 0 Answers
Slow camera move speed? 2 Answers
End of Animation 1 Answer