Skip to content

Commit ac3ae83

Browse files
authored
add MobileCamera.cs
1 parent 541004d commit ac3ae83

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed

Assets/Scripts/Camera/MobileCamera.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// https://forum.unity.com/threads/mobile-touch-to-orbit-pan-and-zoom-camera-without-fix-target-in-one-script.522607/#post-3531342
2+
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
7+
public class MobileCamera : MonoBehaviour
8+
{
9+
public Transform target;
10+
public Vector3 targetOffset;
11+
public float distance = 5.0f;
12+
public float maxDistance = 20;
13+
public float minDistance = .6f;
14+
public float xSpeed = 5.0f;
15+
public float ySpeed = 5.0f;
16+
public int yMinLimit = -80;
17+
public int yMaxLimit = 80;
18+
public float zoomRate = 10.0f;
19+
public float panSpeed = 0.3f;
20+
public float zoomDampening = 5.0f;
21+
22+
private float xDeg = 0.0f;
23+
private float yDeg = 0.0f;
24+
private float currentDistance;
25+
private float desiredDistance;
26+
private Quaternion currentRotation;
27+
private Quaternion desiredRotation;
28+
private Quaternion rotation;
29+
private Vector3 position;
30+
31+
private Vector3 FirstPosition;
32+
private Vector3 SecondPosition;
33+
private Vector3 delta;
34+
private Vector3 lastOffset;
35+
private Vector3 lastOffsettemp;
36+
//private Vector3 CameraPosition;
37+
//private Vector3 Targetposition;
38+
//private Vector3 MoveDistance;
39+
40+
public float offsetSpeed = 0.03f;
41+
42+
void Start() { Init(); }
43+
void OnEnable() { Init(); }
44+
45+
public void Init()
46+
{
47+
//If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
48+
if (!target)
49+
{
50+
GameObject go = new GameObject("Cam Target");
51+
go.transform.position = transform.position + (transform.forward * distance);
52+
target = go.transform;
53+
}
54+
55+
distance = Vector3.Distance(transform.position, target.position);
56+
currentDistance = distance;
57+
desiredDistance = distance;
58+
59+
//be sure to grab the current rotations as starting points.
60+
position = transform.position;
61+
rotation = transform.rotation;
62+
currentRotation = transform.rotation;
63+
desiredRotation = transform.rotation;
64+
65+
//xDeg = Vector3.Angle(Vector3.right, transform.right);
66+
//yDeg = Vector3.Angle(Vector3.up, transform.up);
67+
68+
xDeg = transform.eulerAngles.y + Vector3.Angle(Vector3.right, transform.right);
69+
yDeg = transform.eulerAngles.x + Vector3.Angle(Vector3.up, transform.up);
70+
71+
}
72+
73+
void LateUpdate()
74+
{
75+
// If Control and Alt and Middle button? ZOOM!
76+
if (Input.touchCount == 2)
77+
{
78+
Touch touchZero = Input.GetTouch(0);
79+
Touch touchOne = Input.GetTouch(1);
80+
Vector2 touchZeroPreviousPosition = touchZero.position - touchZero.deltaPosition;
81+
Vector2 touchOnePreviousPosition = touchOne.position - touchOne.deltaPosition;
82+
float prevTouchDeltaMag = (touchZeroPreviousPosition - touchOnePreviousPosition).magnitude;
83+
float TouchDeltaMag = (touchZero.position - touchOne.position).magnitude;
84+
float deltaMagDiff = prevTouchDeltaMag - TouchDeltaMag;
85+
desiredDistance += deltaMagDiff * Time.deltaTime * zoomRate * 0.0025f * Mathf.Abs(desiredDistance);
86+
}
87+
88+
// If middle mouse and left alt are selected? ORBIT
89+
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
90+
{
91+
Vector2 touchposition = Input.GetTouch(0).deltaPosition;
92+
xDeg += touchposition.x * xSpeed * 0.002f;
93+
yDeg -= touchposition.y * ySpeed * 0.002f;
94+
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
95+
96+
}
97+
98+
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
99+
currentRotation = transform.rotation;
100+
rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
101+
transform.rotation = rotation;
102+
103+
if (Input.GetMouseButtonDown(1))
104+
{
105+
FirstPosition = Input.mousePosition;
106+
lastOffset = targetOffset;
107+
}
108+
109+
if (Input.GetMouseButton(1))
110+
{
111+
SecondPosition = Input.mousePosition;
112+
delta = SecondPosition - FirstPosition;
113+
targetOffset = lastOffset + transform.right * delta.x * offsetSpeed + transform.up * delta.y * offsetSpeed;
114+
115+
}
116+
117+
////////Orbit Position
118+
// affect the desired Zoom distance if we roll the scrollwheel
119+
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
120+
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
121+
position = target.position - (rotation * Vector3.forward * currentDistance);
122+
position = position - targetOffset;
123+
transform.position = position;
124+
}
125+
126+
private static float ClampAngle(float angle, float min, float max)
127+
{
128+
if (angle < -360)
129+
angle += 360;
130+
if (angle > 360)
131+
angle -= 360;
132+
return Mathf.Clamp(angle, min, max);
133+
}
134+
}
135+

0 commit comments

Comments
 (0)