@@ -24,27 +24,28 @@ public bool CycleCamera()
24
24
return taps == 3 ;
25
25
}
26
26
27
- public ( Quaternion Rotation , Vector3 Displacement ) CalculateNextTransform (
28
- Vector3 right , Vector3 up , Vector3 forward )
27
+ public ( Quaternion Rotation , Vector3 Displacement )
28
+ CalculateNextTransform ( Vector3 right , Vector3 up , Vector3 forward )
29
29
{
30
- Vector3 displacement = Vector3 . zero ;
31
- Quaternion rotation = Quaternion . identity ;
30
+ // Zero displacement and rotation deltas
31
+ Vector3 displacementDelta = Vector3 . zero ;
32
+ Quaternion rotationDelta = Quaternion . identity ;
32
33
33
34
// Free look
34
35
if ( Input . touchCount == 1 )
35
36
{
36
37
Touch touch = Input . GetTouch ( 0 ) ;
37
38
38
- Vector2 delta = touch . deltaPosition * Camera . LookSpeed ;
39
- Quaternion yRot = Quaternion . AngleAxis ( delta . x , Vector3 . up ) ;
40
- Quaternion xRot = Quaternion . AngleAxis ( - delta . y , right ) ;
41
-
42
- rotation = yRot * xRot ;
39
+ Vector2 scaledTouchDelta = touch . deltaPosition * Camera . LookSpeed ;
40
+ Quaternion yAxisRotation = Quaternion . AngleAxis ( scaledTouchDelta . x , Vector3 . up ) ;
41
+ Quaternion xAxisRotation = Quaternion . AngleAxis ( - scaledTouchDelta . y , right ) ;
43
42
43
+ rotationDelta = yAxisRotation * xAxisRotation ;
44
44
Moving = Moving || touch . tapCount == 2 ;
45
+
45
46
if ( Moving )
46
47
{
47
- displacement = forward * Camera . MoveSpeed * Time . deltaTime ;
48
+ displacementDelta = forward * Camera . MoveSpeed * Time . deltaTime ;
48
49
}
49
50
}
50
51
// Panning + Zooming
@@ -54,46 +55,48 @@ public bool CycleCamera()
54
55
Touch touch1 = Input . GetTouch ( 1 ) ;
55
56
56
57
// Get the sign of each touch in both x and y
57
- float sx0 = Mathf . Sign ( touch0 . deltaPosition . x ) ;
58
- float sy0 = Mathf . Sign ( touch0 . deltaPosition . y ) ;
59
- float sx1 = Mathf . Sign ( touch1 . deltaPosition . x ) ;
60
- float sy1 = Mathf . Sign ( touch1 . deltaPosition . y ) ;
58
+ Vector2 touch0DeltaSign = new Vector2 (
59
+ Mathf . Sign ( touch0 . deltaPosition . x ) , Mathf . Sign ( touch0 . deltaPosition . y ) ) ;
60
+ Vector2 touch1DeltaSign = new Vector2 (
61
+ Mathf . Sign ( touch1 . deltaPosition . x ) , Mathf . Sign ( touch1 . deltaPosition . y ) ) ;
62
+
63
+ // Sum the signs of the x and y input of both touches
64
+ Vector2 twoTouchDeltaSign = touch0DeltaSign + touch1DeltaSign ;
65
+ // If the directions are opposite, zero the input, otherwise choose sign (-1 or +1)
66
+ Vector2 finalTwoTouchDeltaSign = new Vector2 (
67
+ Mathf . Abs ( twoTouchDeltaSign . x ) > 0.0f ? Mathf . Sign ( twoTouchDeltaSign . x ) : 0.0f ,
68
+ Mathf . Abs ( twoTouchDeltaSign . y ) > 0.0f ? Mathf . Sign ( twoTouchDeltaSign . y ) : 0.0f ) ;
61
69
62
70
// Find the min absolute movement in x and y
63
- float x = Mathf . Min (
64
- Mathf . Abs ( touch0 . deltaPosition . x ) , Mathf . Abs ( touch1 . deltaPosition . x ) ) ;
65
- float y = Mathf . Min (
66
- Mathf . Abs ( touch0 . deltaPosition . y ) , Mathf . Abs ( touch1 . deltaPosition . y ) ) ;
67
-
68
- // Sum the signs of the x and y input, if the directions are opposite,
69
- // zero the input, otherwise choose sign (-1 or +1)
70
- float sx = Mathf . Abs ( sx0 + sx1 ) > 0.0f ? Mathf . Sign ( sx0 + sx1 ) : 0.0f ;
71
- float sy = Mathf . Abs ( sy0 + sy1 ) > 0.0f ? Mathf . Sign ( sy0 + sy1 ) : 0.0f ;
71
+ Vector2 minTouchDelta = new Vector2 (
72
+ Mathf . Min ( Mathf . Abs ( touch0 . deltaPosition . x ) , Mathf . Abs ( touch1 . deltaPosition . x ) ) ,
73
+ Mathf . Min ( Mathf . Abs ( touch0 . deltaPosition . y ) , Mathf . Abs ( touch1 . deltaPosition . y ) ) ) ;
72
74
73
75
// Pan in camera space (both touches must move in the same direction)
74
- Vector2 delta = new Vector2 ( x * sx , y * sy ) ;
75
- displacement = ( ( right * delta . x ) + ( up * delta . y ) ) * PanSpeed ;
76
+ Vector2 finalInputDelta = minTouchDelta * finalTwoTouchDeltaSign ;
77
+ displacementDelta = ( ( right * finalInputDelta . x ) + ( up * finalInputDelta . y ) ) * PanSpeed ;
76
78
77
79
// Pinch/Zoom Reference - https://unity3d.com/learn/tutorials/topics/mobile-touch/pinch-zoom
80
+ {
81
+ // Find the position in the previous frame of each touch.
82
+ Vector2 touch0PrevPos = touch0 . position - touch0 . deltaPosition ;
83
+ Vector2 touch1PrevPos = touch1 . position - touch1 . deltaPosition ;
78
84
79
- // Find the position in the previous frame of each touch.
80
- Vector2 touch0PrevPos = touch0 . position - touch0 . deltaPosition ;
81
- Vector2 touch1PrevPos = touch1 . position - touch1 . deltaPosition ;
82
-
83
- // Find the magnitude of the vector (the distance) between the touches in each frame.
84
- float prevTouchDeltaMag = ( touch0PrevPos - touch1PrevPos ) . magnitude ;
85
- float touchDeltaMag = ( touch0 . position - touch1 . position ) . magnitude ;
85
+ // Find the magnitude of the vector (the distance) between the touches in each frame.
86
+ float prevTouchDeltaMag = ( touch0PrevPos - touch1PrevPos ) . magnitude ;
87
+ float touchDeltaMag = ( touch0 . position - touch1 . position ) . magnitude ;
86
88
87
- // Find the difference in the distances between each frame.
88
- float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag ;
89
+ // Find the difference in the distances between each frame.
90
+ float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag ;
89
91
90
- displacement -= forward * deltaMagnitudeDiff * ZoomSpeed ;
92
+ displacementDelta -= forward * deltaMagnitudeDiff * ZoomSpeed ;
93
+ }
91
94
}
92
95
else
93
96
{
94
97
Moving = false ;
95
98
}
96
99
97
- return ( rotation , displacement ) ;
100
+ return ( rotationDelta , displacementDelta ) ;
98
101
}
99
102
}
0 commit comments