Angular
How do I use Angular
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: 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: PhotonRigidbody2DViewEditor.cs
Copy
7 public override void OnInspectorGUI()
8 {
9 PhotonGUI.ContainerHeader("Options");
10
11 Rect containerRect = PhotonGUI.ContainerBody(EditorGUIUtility.singleLineHeight*2 + 10);
12
13 Rect propertyRect = new Rect(containerRect.xMin + 5, containerRect.yMin + 5, containerRect.width, EditorGUIUtility.singleLineHeight);
14 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeVelocity"), new GUIContent("Synchronize Velocity"));
15
16 propertyRect.y += EditorGUIUtility.singleLineHeight;
17 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeAngularVelocity"), new GUIContent("Synchronize Angular Velocity"));
18 }
File name: PhotonRigidbodyViewEditor.cs
Copy
7 public override void OnInspectorGUI()
8 {
9 PhotonGUI.ContainerHeader("Options");
10
11 Rect containerRect = PhotonGUI.ContainerBody(EditorGUIUtility.singleLineHeight*2 + 10);
12
13 Rect propertyRect = new Rect(containerRect.xMin + 5, containerRect.yMin + 5, containerRect.width, EditorGUIUtility.singleLineHeight);
14 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeVelocity"), new GUIContent("Synchronize Velocity"));
15
16 propertyRect.y += EditorGUIUtility.singleLineHeight;
17 EditorGUI.PropertyField(propertyRect, serializedObject.FindProperty("m_SynchronizeAngularVelocity"), new GUIContent("Synchronize Angular Velocity"));
18 }
File name: PhotonRigidbody2DView.cs
Copy
32 void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33 {
34 if( stream.isWriting == true )
35 {
36 if( m_SynchronizeVelocity == true )
37 {
38 stream.SendNext( m_Body.velocity );
39 }
40
41 if( m_SynchronizeAngularVelocity == true )
42 {
43 stream.SendNext( m_Body.angularVelocity );
44 }
45 }
46 else
47 {
48 if( m_SynchronizeVelocity == true )
49 {
50 m_Body.velocity = (Vector2)stream.ReceiveNext();
51 }
52
53 if( m_SynchronizeAngularVelocity == true )
54 {
55 m_Body.angularVelocity = (float)stream.ReceiveNext();
56 }
57 }
58 }
File name: PhotonRigidbodyView.cs
Copy
32 void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33 {
34 if( stream.isWriting == true )
35 {
36 if( m_SynchronizeVelocity == true )
37 {
38 stream.SendNext( m_Body.velocity );
39 }
40
41 if( m_SynchronizeAngularVelocity == true )
42 {
43 stream.SendNext( m_Body.angularVelocity );
44 }
45 }
46 else
47 {
48 if( m_SynchronizeVelocity == true )
49 {
50 m_Body.velocity = (Vector3)stream.ReceiveNext();
51 }
52
53 if( m_SynchronizeAngularVelocity == true )
54 {
55 m_Body.angularVelocity = (Vector3)stream.ReceiveNext();
56 }
57 }
58 }
File name: PhotonView.cs
Copy
348 internal protected void DeserializeComponent( Component component, PhotonStream stream, PhotonMessageInfo info )
349 {
350 if( component == null )
351 {
352 return;
353 }
354
355 // Use incoming data according to observed type
356 if( component is MonoBehaviour )
357 {
358 ExecuteComponentOnSerialize( component, stream, info );
359 }
360 else if( component is Transform )
361 {
362 Transform trans = (Transform)component;
363
364 switch( onSerializeTransformOption )
365 {
366 case OnSerializeTransform.All:
367 trans.localPosition = (Vector3)stream.ReceiveNext();
368 trans.localRotation = (Quaternion)stream.ReceiveNext();
369 trans.localScale = (Vector3)stream.ReceiveNext();
370 break;
371 case OnSerializeTransform.OnlyPosition:
372 trans.localPosition = (Vector3)stream.ReceiveNext();
373 break;
374 case OnSerializeTransform.OnlyRotation:
375 trans.localRotation = (Quaternion)stream.ReceiveNext();
376 break;
377 case OnSerializeTransform.OnlyScale:
378 trans.localScale = (Vector3)stream.ReceiveNext();
379 break;
380 case OnSerializeTransform.PositionAndRotation:
381 trans.localPosition = (Vector3)stream.ReceiveNext();
382 trans.localRotation = (Quaternion)stream.ReceiveNext();
383 break;
384 }
385 }
386 else if( component is Rigidbody )
387 {
388 Rigidbody rigidB = (Rigidbody)component;
389
390 switch( onSerializeRigidBodyOption )
391 {
392 case OnSerializeRigidBody.All:
393 rigidB.velocity = (Vector3)stream.ReceiveNext();
394 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
395 break;
396 case OnSerializeRigidBody.OnlyAngularVelocity:
397 rigidB.angularVelocity = (Vector3)stream.ReceiveNext();
398 break;
399 case OnSerializeRigidBody.OnlyVelocity:
400 rigidB.velocity = (Vector3)stream.ReceiveNext();
401 break;
402 }
403 }
404 else if( component is Rigidbody2D )
405 {
406 Rigidbody2D rigidB = (Rigidbody2D)component;
407
408 switch( onSerializeRigidBodyOption )
409 {
410 case OnSerializeRigidBody.All:
411 rigidB.velocity = (Vector2)stream.ReceiveNext();
412 rigidB.angularVelocity = (float)stream.ReceiveNext();
413 break;
414 case OnSerializeRigidBody.OnlyAngularVelocity:
415 rigidB.angularVelocity = (float)stream.ReceiveNext();
416 break;
417 case OnSerializeRigidBody.OnlyVelocity:
418 rigidB.velocity = (Vector2)stream.ReceiveNext();
419 break;
420 }
421 }
422 else
423 {
424 Debug.LogError( "Type of observed is unknown when receiving." );
425 }
426 }
File name: PhotonView.cs
Copy
428 internal protected void SerializeComponent( Component component, PhotonStream stream, PhotonMessageInfo info )
429 {
430 if( component == null )
431 {
432 return;
433 }
434
435 if( component is MonoBehaviour )
436 {
437 ExecuteComponentOnSerialize( component, stream, info );
438 }
439 else if( component is Transform )
440 {
441 Transform trans = (Transform)component;
442
443 switch( onSerializeTransformOption )
444 {
445 case OnSerializeTransform.All:
446 stream.SendNext( trans.localPosition );
447 stream.SendNext( trans.localRotation );
448 stream.SendNext( trans.localScale );
449 break;
450 case OnSerializeTransform.OnlyPosition:
451 stream.SendNext( trans.localPosition );
452 break;
453 case OnSerializeTransform.OnlyRotation:
454 stream.SendNext( trans.localRotation );
455 break;
456 case OnSerializeTransform.OnlyScale:
457 stream.SendNext( trans.localScale );
458 break;
459 case OnSerializeTransform.PositionAndRotation:
460 stream.SendNext( trans.localPosition );
461 stream.SendNext( trans.localRotation );
462 break;
463 }
464 }
465 else if( component is Rigidbody )
466 {
467 Rigidbody rigidB = (Rigidbody)component;
468
469 switch( onSerializeRigidBodyOption )
470 {
471 case OnSerializeRigidBody.All:
472 stream.SendNext( rigidB.velocity );
473 stream.SendNext( rigidB.angularVelocity );
474 break;
475 case OnSerializeRigidBody.OnlyAngularVelocity:
476 stream.SendNext( rigidB.angularVelocity );
477 break;
478 case OnSerializeRigidBody.OnlyVelocity:
479 stream.SendNext( rigidB.velocity );
480 break;
481 }
482 }
483 else if( component is Rigidbody2D )
484 {
485 Rigidbody2D rigidB = (Rigidbody2D)component;
486
487 switch( onSerializeRigidBodyOption )
488 {
489 case OnSerializeRigidBody.All:
490 stream.SendNext( rigidB.velocity );
491 stream.SendNext( rigidB.angularVelocity );
492 break;
493 case OnSerializeRigidBody.OnlyAngularVelocity:
494 stream.SendNext( rigidB.angularVelocity );
495 break;
496 case OnSerializeRigidBody.OnlyVelocity:
497 stream.SendNext( rigidB.velocity );
498 break;
499 }
500 }
501 else
502 {
503 Debug.LogError( "Observed type is not serializable: " + component.GetType() );
504 }
505 }
Angular 109 lượt xem
Gõ tìm kiếm nhanh...