- Home /
Any help with prevent camera from passing a specific coordinate?
I try to prevent my camera from moving for ever, so I want to add somthing like "borders". I tried this, but it gives me errors. How should I fix it?
if(Camera.main.transform.position >= Vector3(worldBorderX, transform.position.y, transform.position.z))
{
Camera.main.transform.position = Vector3(worldBorderX, transform.position.y, transform.position.z);
}
if(Camera.main.transform.position >= Vector3(transform.position.x, transform.position.y, worldBorderZ))
{
Camera.main.transform.position = Vector3(transform.position.x, transform.position.y, worldBorderZ);
}
if(Camera.main.transform.position <= Vector3(-worldBorderX, transform.position.y, transform.position.z))
{
Camera.main.transform.position = Vector3(-worldBorderX, transform.position.y, transform.position.z);
}
if(Camera.main.transform.position <= Vector3(transform.position.x, transform.position.y, -worldBorderZ))
{
Camera.main.transform.position = Vector3(transform.position.x, transform.position.y, -worldBorderZ);
}
Answer by Landern · Mar 14, 2013 at 02:15 PM
assuming you are using Javascript/UnityScript. You may also want to edit your original post and add the errors.
if(Camera.main.transform.position.x >= worldBorderX)
{
Camera.main.transform.position.x = worldBorderX;
}
if(Camera.main.transform.position.z >= worldBorderZ)
{
Camera.main.transform.position.z = worldBorderZ;
}
if(Camera.main.transform.position.x <= -worldBorderX)
{
Camera.main.transform.position.x = -worldBorderX;
}
if(Camera.main.transform.position.z <= -worldBorderZ)
{
Camera.main.transform.position.z = -worldBorderZ;
}
or
$$anonymous$$athf.Clamp( Camera.main.transform.position.x, -worldBorderX, worldBorderX );
$$anonymous$$athf.Clamp( Camera.main.transform.position.z, -worldBorderZ, worldBorderZ );
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
The above is still a good answer, upvoted.
You're right Alucardj and less bloat, upvote for you sir.
Your answer
Follow this Question
Related Questions
Trouble converting transform.position to C# 1 Answer
nullifying targetObject after a Smooth LookAt Transition 2 Answers
how do i move ludo token to right side 0 Answers
How to make Camera position Independent of its Rotation? 1 Answer
How to rotate an object to face the direction it's going? 1 Answer