CameraTransformCamera









How do I use Camera Transform Camera
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PickupCamera.cs Copy
41     void OnEnable()
42     {
43         if( this.photonView != null && !this.photonView.isMine )
44         {
45             this.enabled = false;
46             return;
47         }
48
49         if( !cameraTransform && Camera.main )
50             cameraTransform = Camera.main.transform;
51         if( !cameraTransform )
52         {
53             Debug.Log( "Please assign a camera to the ThirdPersonCamera script." );
54             enabled = false;
55         }
56
57         m_CameraTransformCamera = cameraTransform.GetComponent();
58
59
60         _target = transform;
61         if( _target )
62         {
63             controller = _target.GetComponent();
64         }
65
66         if( controller )
67         {
68             CharacterController characterController = (CharacterController)_target.GetComponent();
69             centerOffset = characterController.bounds.center - _target.position;
70             headOffset = centerOffset;
71             headOffset.y = characterController.bounds.max.y - _target.position.y;
72         }
73         else
74             Debug.Log( "Please assign a target to the camera that has a ThirdPersonController script attached." );
75
76
77         Cut( _target, centerOffset );
78     }
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: ThirdPersonCamera.cs Copy
40     void OnEnable()
41     {
42         if( !cameraTransform && Camera.main )
43             cameraTransform = Camera.main.transform;
44         if( !cameraTransform )
45         {
46             Debug.Log( "Please assign a camera to the ThirdPersonCamera script." );
47             enabled = false;
48         }
49
50         m_CameraTransformCamera = cameraTransform.GetComponent();
51
52         _target = transform;
53         if( _target )
54         {
55             controller = _target.GetComponent();
56         }
57
58         if( controller )
59         {
60             CharacterController characterController = (CharacterController)_target.GetComponent();
61             centerOffset = characterController.bounds.center - _target.position;
62             headOffset = centerOffset;
63             headOffset.y = characterController.bounds.max.y - _target.position.y;
64         }
65         else
66             Debug.Log( "Please assign a target to the camera that has a ThirdPersonController script attached." );
67
68
69         Cut( _target, centerOffset );
70     }
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     }

CameraTransformCamera 108 lượt xem

Gõ tìm kiếm nhanh...