- Home /
Question by
zkry-akgul · Mar 30, 2018 at 07:00 AM ·
c#camerarotationmobilecamera rotate
Limit camera RotateAround for an UAV game
Hi, i am building a mobile UAV game and i want to limit camera's rotation. What is the best way to do this perfectly? Here is my code for using rotate camera:
public class CameraMove : MonoBehaviour
{
//
// VARIABLES
//
public Camera UAVCam;
public float turnSpeed = 4.0f; // Speed of camera turning when touch moves in along an axis
private Vector2 mouseOrigin; // Position of touch when touch dragging starts
private bool isRotating; // Is the camera being rotated?
//
// UPDATE
//
void Update ()
{
// Get the first touch
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
// Get touch origin
mouseOrigin = Input.GetTouch(0).position;
isRotating = true;
}
// Disable movements on touch release
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)) isRotating=false;
// Rotate camera along X and Y axis
if (isRotating) {
Vector2 temp = Input.GetTouch (0).position - mouseOrigin;
Vector3 pos =UAVCam.ScreenToViewportPoint(new Vector3(temp.x,temp.y,0.0f));
UAVCam.transform.RotateAround(UAVCam.transform.position, UAVCam.transform.right, -pos.y * turnSpeed);
UAVCam.transform.RotateAround(UAVCam.transform.position, Vector3.up, pos.x * turnSpeed);
}
}
}
Thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
maximum angle for camera 0 Answers
Coding noob struggles to implement first-Person camera rotation. 1 Answer
Third person follow camera on a sphere 1 Answer
FPS Camera Control rotation limits 1 Answer