- Home /
camera RotateAround, limit angles.
hi, I am working on a app with a globe, I need to navigate the globe by dragging it google maps style.
That part I have working by using this method : Link
Now my problem is when navigating it, the camera gets rotated in some directions that makes look like the globe is turned upside down etc. I want to limit my globe (the camera) to always be the right direction (north up, south down). Also I want to be able to limit how high/low, the user can navigate the globe. No need to be able to see straight down/up at the poles.
All help and hints appreciated :-)
Thanks, Per
Something like this, orange area being the allowed area:
my code so far :
private void HandleDrag(Vector3 touchPosition)
{
_currentTouchPos = getTouchHit(touchPosition);
RotateCamera((Vector3)_touchStartPos, (Vector3)_currentTouchPos);
}
private void RotateCamera(Vector3 dragStartPosition, Vector3 dragEndPosition)
{
// in case the spehre model is not a perfect sphere..
dragEndPosition = dragEndPosition.normalized * SphereRadius;
dragStartPosition = dragStartPosition.normalized * SphereRadius;
// calc a vertical vector to rotate around..
var cross = Vector3.Cross(dragEndPosition, dragStartPosition);
// calc the angle for the rotation..
var angle = Vector3.SignedAngle(dragEndPosition, dragStartPosition, cross);
// roatate around the vector..
Camera.main.transform.RotateAround(transform.position, cross, angle);
}
private Vector3? getTouchHit(Vector3 checkPosition)
{
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(checkPosition),out hit))
{
return hit.point;
}
return null;
}
Your answer
Follow this Question
Related Questions
Limit Y axis rotation of camera 1 Answer
Help With Arrow Camera Script 2 Answers
Limit Y Axis transform.RotateAround 1 Answer