Angle
How do I use Angle
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PickupCamera.cs
Copy
86 float AngleDistance( float a, float b )
87 {
88 a = Mathf.Repeat( a, 360 );
89 b = Mathf.Repeat( b, 360 );
90
91 return Mathf.Abs( b - a );
92 }
File name: PickupCamera.cs
Copy
94 void Apply( Transform dummyTarget, Vector3 dummyCenter )
95 {
96 // Early out if we don't have a target
97 if( !controller )
98 return;
99
100 Vector3 targetCenter = _target.position + centerOffset;
101 Vector3 targetHead = _target.position + headOffset;
102
103 // DebugDrawStuff();
104
105 // Calculate the current & target rotation angles
106 float originalTargetAngle = _target.eulerAngles.y;
107 float currentAngle = cameraTransform.eulerAngles.y;
108
109 // Adjust real target angle when camera is locked
110 float targetAngle = originalTargetAngle;
111
112 // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
113 // It will stop snapping when it reaches the target
114 if( Input.GetButton( "Fire2" ) )
115 snap = true;
116
117 if( snap )
118 {
119 // We are close to the target, so we can stop snapping now!
120 if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
121 snap = false;
122
123 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
124 }
125 // Normal camera motion
126 else
127 {
128 if( controller.GetLockCameraTimer() < lockCameraTimeout )
129 {
130 targetAngle = currentAngle;
131 }
132
133 // Lock the camera when moving backwards!
134 // * It is really confusing to do 180 degree spins when turning around.
135 if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
136 targetAngle += 180;
137
138 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
139 }
140
141
142 // When jumping don't move camera upwards but only down!
143 if( controller.IsJumping() )
144 {
145 // We'd be moving the camera upwards, do that only if it's really high
146 float newTargetHeight = targetCenter.y + height;
147 if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
148 targetHeight = targetCenter.y + height;
149 }
150 // When walking always update the target height
151 else
152 {
153 targetHeight = targetCenter.y + height;
154 }
155
156 // Damp the height
157 float currentHeight = cameraTransform.position.y;
158 currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
159
160 // Convert the angle into a rotation, by which we then reposition the camera
161 Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
162
163 // Set the position of the camera on the x-z plane to:
164 // distance meters behind the target
165 cameraTransform.position = targetCenter;
166 cameraTransform.position += currentRotation * Vector3.back * distance;
167
168 // Set the height of the camera
169 cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
170
171 // Always look at the target
172 SetUpRotation( targetCenter, targetHead );
173 }
File name: PickupCamera.cs
Copy
198 void SetUpRotation( Vector3 centerPos, Vector3 headPos )
199 {
200 // Now it's getting hairy. The devil is in the details here, the big issue is jumping of course.
201 // * When jumping up and down we don't want to center the guy in screen space.
202 // This is important to give a feel for how high you jump and avoiding large camera movements.
203 //
204 // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth.
205 //
206 // So here is what we will do:
207 //
208 // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis
209 // 2. When grounded we make him be centered
210 // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold
211 // 4. When landing we smoothly interpolate towards centering him on screen
212 Vector3 cameraPos = cameraTransform.position;
213 Vector3 offsetToCenter = centerPos - cameraPos;
214
215 // Generate base rotation only around y-axis
216 Quaternion yRotation = Quaternion.LookRotation( new Vector3( offsetToCenter.x, 0, offsetToCenter.z ) );
217
218 Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
219 cameraTransform.rotation = yRotation * Quaternion.LookRotation( relativeOffset );
220
221 // Calculate the projected center position and top position in world space
222 Ray centerRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, 0.5f, 1 ) );
223 Ray topRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, clampHeadPositionScreenSpace, 1 ) );
224
225 Vector3 centerRayPos = centerRay.GetPoint( distance );
226 Vector3 topRayPos = topRay.GetPoint( distance );
227
228 float centerToTopAngle = Vector3.Angle( centerRay.direction, topRay.direction );
229
230 float heightToAngle = centerToTopAngle / ( centerRayPos.y - topRayPos.y );
231
232 float extraLookAngle = heightToAngle * ( centerRayPos.y - centerPos.y );
233 if( extraLookAngle < centerToTopAngle )
234 {
235 extraLookAngle = 0;
236 }
237 else
238 {
239 extraLookAngle = extraLookAngle - centerToTopAngle;
240 cameraTransform.rotation *= Quaternion.Euler( -extraLookAngle, 0, 0 );
241 }
242 }
File name: RPGCamera.cs
Copy
58 void UpdateRotation()
59 {
60 if( Input.GetMouseButton( 0 ) == true || Input.GetMouseButton( 1 ) == true )
61 {
62 transform.Rotate( 0, Input.GetAxis( "Mouse X" ) * TurnModifier, 0 );
63 }
64
65 if( Input.GetMouseButton( 1 ) == true && Target != null )
66 {
67 Target.rotation = Quaternion.Euler( 0, transform.rotation.eulerAngles.y, 0 );
68 }
69 }
File name: ThirdPersonCamera.cs
Copy
78 float AngleDistance( float a, float b )
79 {
80 a = Mathf.Repeat( a, 360 );
81 b = Mathf.Repeat( b, 360 );
82
83 return Mathf.Abs( b - a );
84 }
File name: ThirdPersonCamera.cs
Copy
86 void Apply( Transform dummyTarget, Vector3 dummyCenter )
87 {
88 // Early out if we don't have a target
89 if( !controller )
90 return;
91
92 Vector3 targetCenter = _target.position + centerOffset;
93 Vector3 targetHead = _target.position + headOffset;
94
95 // DebugDrawStuff();
96
97 // Calculate the current & target rotation angles
98 float originalTargetAngle = _target.eulerAngles.y;
99 float currentAngle = cameraTransform.eulerAngles.y;
100
101 // Adjust real target angle when camera is locked
102 float targetAngle = originalTargetAngle;
103
104 // When pressing Fire2 (alt) the camera will snap to the target direction real quick.
105 // It will stop snapping when it reaches the target
106 if( Input.GetButton( "Fire2" ) )
107 snap = true;
108
109 if( snap )
110 {
111 // We are close to the target, so we can stop snapping now!
112 if( AngleDistance( currentAngle, originalTargetAngle ) < 3.0f )
113 snap = false;
114
115 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, snapSmoothLag, snapMaxSpeed );
116 }
117 // Normal camera motion
118 else
119 {
120 if( controller.GetLockCameraTimer() < lockCameraTimeout )
121 {
122 targetAngle = currentAngle;
123 }
124
125 // Lock the camera when moving backwards!
126 // * It is really confusing to do 180 degree spins when turning around.
127 if( AngleDistance( currentAngle, targetAngle ) > 160 && controller.IsMovingBackwards() )
128 targetAngle += 180;
129
130 currentAngle = Mathf.SmoothDampAngle( currentAngle, targetAngle, ref angleVelocity, angularSmoothLag, angularMaxSpeed );
131 }
132
133
134 // When jumping don't move camera upwards but only down!
135 if( controller.IsJumping() )
136 {
137 // We'd be moving the camera upwards, do that only if it's really high
138 float newTargetHeight = targetCenter.y + height;
139 if( newTargetHeight < targetHeight || newTargetHeight - targetHeight > 5 )
140 targetHeight = targetCenter.y + height;
141 }
142 // When walking always update the target height
143 else
144 {
145 targetHeight = targetCenter.y + height;
146 }
147
148 // Damp the height
149 float currentHeight = cameraTransform.position.y;
150 currentHeight = Mathf.SmoothDamp( currentHeight, targetHeight, ref heightVelocity, heightSmoothLag );
151
152 // Convert the angle into a rotation, by which we then reposition the camera
153 Quaternion currentRotation = Quaternion.Euler( 0, currentAngle, 0 );
154
155 // Set the position of the camera on the x-z plane to:
156 // distance meters behind the target
157 cameraTransform.position = targetCenter;
158 cameraTransform.position += currentRotation * Vector3.back * distance;
159
160 // Set the height of the camera
161 cameraTransform.position = new Vector3( cameraTransform.position.x, currentHeight, cameraTransform.position.z );
162
163 // Always look at the target
164 SetUpRotation( targetCenter, targetHead );
165 }
File name: ThirdPersonCamera.cs
Copy
190 void SetUpRotation( Vector3 centerPos, Vector3 headPos )
191 {
192 // Now it's getting hairy. The devil is in the details here, the big issue is jumping of course.
193 // * When jumping up and down we don't want to center the guy in screen space.
194 // This is important to give a feel for how high you jump and avoiding large camera movements.
195 //
196 // * At the same time we dont want him to ever go out of screen and we want all rotations to be totally smooth.
197 //
198 // So here is what we will do:
199 //
200 // 1. We first find the rotation around the y axis. Thus he is always centered on the y-axis
201 // 2. When grounded we make him be centered
202 // 3. When jumping we keep the camera rotation but rotate the camera to get him back into view if his head is above some threshold
203 // 4. When landing we smoothly interpolate towards centering him on screen
204 Vector3 cameraPos = cameraTransform.position;
205 Vector3 offsetToCenter = centerPos - cameraPos;
206
207 // Generate base rotation only around y-axis
208 Quaternion yRotation = Quaternion.LookRotation( new Vector3( offsetToCenter.x, 0, offsetToCenter.z ) );
209
210 Vector3 relativeOffset = Vector3.forward * distance + Vector3.down * height;
211 cameraTransform.rotation = yRotation * Quaternion.LookRotation( relativeOffset );
212
213 // Calculate the projected center position and top position in world space
214 Ray centerRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, 0.5f, 1 ) );
215 Ray topRay = m_CameraTransformCamera.ViewportPointToRay( new Vector3( 0.5f, clampHeadPositionScreenSpace, 1 ) );
216
217 Vector3 centerRayPos = centerRay.GetPoint( distance );
218 Vector3 topRayPos = topRay.GetPoint( distance );
219
220 float centerToTopAngle = Vector3.Angle( centerRay.direction, topRay.direction );
221
222 float heightToAngle = centerToTopAngle / ( centerRayPos.y - topRayPos.y );
223
224 float extraLookAngle = heightToAngle * ( centerRayPos.y - centerPos.y );
225 if( extraLookAngle < centerToTopAngle )
226 {
227 extraLookAngle = 0;
228 }
229 else
230 {
231 extraLookAngle = extraLookAngle - centerToTopAngle;
232 cameraTransform.rotation *= Quaternion.Euler( -extraLookAngle, 0, 0 );
233 }
234 }
File name: Extensions.cs
Copy
45 public static bool AlmostEquals(this Quaternion target, Quaternion second, float maxAngle)
46 {
47 return Quaternion.Angle(target, second) < maxAngle;
48 }
File name: RotateCamera.cs
Copy
30 void Start() {
31 pivotEulers = pivotTransform.rotation.eulerAngles;
32 EnableInput();
33 inputManager = InputManager.Instance;
34 }
File name: RotateCamera.cs
Copy
48 protected void Update() {
49 if (!rotate) return;
50 lookAngleY += inputManager.MouseAxis.x * horizAngleMove;
51 newRotY = Quaternion.Euler(0f,lookAngleY,0f);
52
53 lookAngleX += inputManager.MouseAxis.y * vertAngleMove;
54 lookAngleX = Mathf.Clamp(lookAngleX, -xAngleMin, xAngleMax);
55 newRotX = Quaternion.Euler(lookAngleX, pivotEulers.y, pivotEulers.z);
56
57 if (turnSmoothing > 0) {
58 pivotTransform.localRotation = Quaternion.Slerp(pivotTransform.localRotation, newRotX, turnSmoothing * Time.deltaTime);
59 transform.localRotation = Quaternion.Slerp(transform.localRotation, newRotY, turnSmoothing * Time.deltaTime);
60 } else {
61 transform.localRotation = newRotY;
62 pivotTransform.localRotation = newRotX;
63 }
64 }
Download file with original file name:Angle
Angle 119 lượt xem
Gõ tìm kiếm nhanh...