- Home /
Main camera zoom, rotation, position (android)
Hello Unity Community,
I have faced a problem. What I am trying to do is to rotate and reposition camera as I pinch zoom in and out. For example in the beginning I have top view, i.e. MainCamera's rotation is 90,0,0; when I pinch zoom I change the fieldOfView and the problem is that camre is not facing the game board when it rotates into perspective position, so I have added the lerping of the position but it doesn't look that smooth. If anyone knows a better way to do this I will appretiate the answer. Here is my code:
void Update () {
if (Input.touchCount == 2) {
Touch touch = Input.GetTouch(0);
Touch touch1 = Input.GetTouch(1);
if (touch.phase == TouchPhase.Moved && touch1.phase == TouchPhase.Moved) {
Vector2 curDist = touch.position - touch1.position;
Vector2 prevDist = (touch.position - touch.deltaPosition) - (touch1.position - touch1.deltaPosition);
float delta = (curDist.magnitude - prevDist.magnitude)/50;
if (delta < 0) {
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView + 1,30,45);
rotation.x = Mathf.Clamp(rotation.x + 2,40,90);
Camera.main.transform.rotation = Quaternion.Euler(rotation.x, 0, 0);
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(0, 110, 0), 0.1f);
}
if (delta > 0) {
Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView - 1,30,45);
rotation.x = Mathf.Clamp(rotation.x - 2,40,90);
Camera.main.transform.rotation = Quaternion.Euler(rotation.x, 0, 0);
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, new Vector3(0, 50, -65), 0.1f);
}
}
}
Thank you in advance. P.S. Answers can be in both C# and JS.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using scripts coded in C# on android projects 1 Answer
C++ windows DLL and Android? 1 Answer