- Home /
How to restrict Perspective view camera limits should be inside my plane?
Apologies if this has been asked before, but it's slightly different. I am using Perceptive view camera. When i am panning cam is moving out of my plane How can i restrict bounds When cam is crosses edges of plane.
Here is the snippet of the code what i am doing. Please Help me On this
using two planes 1)primary Plane(Bigger than Cam view) 2)Secondary plane (PrimaryPlane - (cam Width/2)).
For boundaries just checking with a raycast from cam centre position to a Secondary plan (PrimaryPlane - (cam Width/2)).If ray cast crosses the plane i am keeping cam in. but when we are swiping faster cam is going out of the Primary Plane bounds. how can i restrict bounds @ Primary Plane edges ??
public class PerspectiveCameraController : MonoBehaviour {
void LateUpdate()
{
getCamBoundaries();
}
void Update ()
{
if( Input.touchCount > 0)
{
isCanMove = false;
if (Input.touchCount == 1)
{
PAN();
}
}
}
void PAN(){
if(Input.GetTouch (0).phase != TouchPhase.Moved)
{
initialPanPosition = Input.touches[0].position - Input.touches[0].deltaPosition;
return;
}
Vector2 touchDeltaPosition = Input.touches[0].position - Input.touches[0].deltaPosition;
Vector2 panDistance = ((initialPanPosition - touchDeltaPosition)/Input.touches[0].deltaTime);
Vector3 diff = Vector3.zero;
diff.x = Mathf.Clamp(panDistance.x * panSpeed * Time.deltaTime, -10f,10f);
diff.y = Mathf.Clamp(panDistance.y * panSpeed * Time.deltaTime, -10f,10f);
if(!float.IsNaN(diff.x) && !float.IsNaN(diff.y))
{
print ("diff.x "+diff.x+" diff.y "+diff.y);
transform.position += (transform.right * diff.x) + (transform.up * diff.y);
initialPanPosition = touchDeltaPosition;
}
}
void getCamBoundaries()
{
Plane thePlane = new Plane( Vector3.up, Vector3.zero ); // The XZ plane the game is played on
Ray ray ;
ray = Camera.main.ViewportPointToRay(new Vector3(0.5f,0.5f,0f));
thePlane.Raycast(ray, out distance); // Find the distance to Center...
camCenterpos = ray.GetPoint(distance);
getBoundsOfCenter(camCenterpos);
}
void getBoundsOfCenter(Vector3 _camCenterpos)
{
RaycastHit hitInfo;
if(Physics.Raycast(updateTransfromPosition, transform.forward, out hitInfo, Mathf.Infinity, CamBoundary.value))
{
isCamProperOnMAP = true;
isCamOutofbounds = false;
isCamMoving = false;
camCenterPosInsideBoundry = _camCenterpos;
camPosition = transform.position;
//camCurrentPosition = transform.position;
Debug.DrawLine(updateTransfromPosition, _camCenterpos, Color.green, 1f);
}
else{
isCamProperOnMAP = false;
camCurrentPosition = transform.position;
isCamOutofbounds = true;
camCurrentPosition = transform.position;
}
}
}
Please Let me know is there any other process to solve this.
Your answer
Follow this Question
Related Questions
Camera stucks at the spawn position 1 Answer
*Please help, my first person camera goes all over the place after enabling weapon collider* 0 Answers
Camera that follows rotating object with mouse input 0 Answers
I need to find a way to pan my camera around an object. 1 Answer
How to prevent camera from clipping through walls with raycasts? 0 Answers