CharacterController









How do I use Character Controller
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: PickupController.cs Copy
151     void Update()
152     {
153         if (isControllable)
154         {
155             if (Input.GetButtonDown("Jump"))
156             {
157                 lastJumpButtonTime = Time.time;
158             }
159
160             UpdateSmoothedMovementDirection();
161
162             // Apply gravity
163             // - extra power jump modifies gravity
164             // - controlledDescent mode modifies gravity
165             ApplyGravity();
166
167             // Apply jumping logic
168             ApplyJumping();
169
170
171             // Calculate actual motion
172             Vector3 movement = moveDirection * moveSpeed + new Vector3(0, verticalSpeed, 0) + inAirVelocity;
173             movement *= Time.deltaTime;
174
175             //Debug.Log(movement.x.ToString("0.000") + ":" + movement.z.ToString("0.000"));
176
177             // Move the controller
178             CharacterController controller = GetComponent();
179             collisionFlags = controller.Move(movement);
180
181         }
182
183         // PUN: if a remote position is known, we smooth-move to it (being late(r) but smoother)
184         if (this.remotePosition != Vector3.zero)
185         {
186             transform.position = Vector3.Lerp(transform.position, this.remotePosition, Time.deltaTime * this.RemoteSmoothing);
187         }
188
189         velocity = (transform.position - lastPos)*25;
190
191         // ANIMATION sector
192         if (_animation)
193         {
194             if (_characterState == PickupCharacterState.Jumping)
195             {
196                 if (!jumpingReachedApex)
197                 {
198                     _animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed;
199                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
200                     _animation.CrossFade(jumpPoseAnimation.name);
201                 }
202                 else
203                 {
204                     _animation[jumpPoseAnimation.name].speed = -landAnimationSpeed;
205                     _animation[jumpPoseAnimation.name].wrapMode = WrapMode.ClampForever;
206                     _animation.CrossFade(jumpPoseAnimation.name);
207                 }
208             }
209             else
210             {
211                 if (_characterState == PickupCharacterState.Idle)
212                 {
213                     _animation.CrossFade(idleAnimation.name);
214                 }
215                 else if (_characterState == PickupCharacterState.Running)
216                 {
217                     _animation[runAnimation.name].speed = runMaxAnimationSpeed;
218                     if (this.isControllable)
219                     {
220                         _animation[runAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, runMaxAnimationSpeed);
221                     }
222                     _animation.CrossFade(runAnimation.name);
223                 }
224                 else if (_characterState == PickupCharacterState.Trotting)
225                 {
226                     _animation[walkAnimation.name].speed = trotMaxAnimationSpeed;
227                     if (this.isControllable)
228                     {
229                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, trotMaxAnimationSpeed);
230                     }
231                     _animation.CrossFade(walkAnimation.name);
232                 }
233                 else if (_characterState == PickupCharacterState.Walking)
234                 {
235                     _animation[walkAnimation.name].speed = walkMaxAnimationSpeed;
236                     if (this.isControllable)
237                     {
238                         _animation[walkAnimation.name].speed = Mathf.Clamp(velocity.magnitude, 0.0f, walkMaxAnimationSpeed);
239                     }
240                     _animation.CrossFade(walkAnimation.name);
241                 }
242
243                 if (_characterState != PickupCharacterState.Running)
244                 {
245                     _animation[runAnimation.name].time = 0.0f;
246                 }
247             }
248         }
249         // ANIMATION sector
250
251         // Set rotation to the move direction
252         if (IsGrounded())
253         {
254             // a specialty of this controller: you can disable rotation!
255             if (DoRotate)
256             {
257                 transform.rotation = Quaternion.LookRotation(moveDirection);
258             }
259         }
260         else
261         {
262             /* This causes choppy behaviour when colliding with SIDES
263              * Vector3 xzMove = velocity;
264             xzMove.y = 0;
265             if (xzMove.sqrMagnitude > 0.001f)
266             {
267                 transform.rotation = Quaternion.LookRotation(xzMove);
268             }*/
269         }
270
271         // We are in jump mode but just became grounded
272         if (IsGrounded())
273         {
274             lastGroundedTime = Time.time;
275             inAirVelocity = Vector3.zero;
276             if (jumping)
277             {
278                 jumping = false;
279                 SendMessage("DidLand", SendMessageOptions.DontRequireReceiver);
280             }
281         }
282
283         lastPos = transform.position;
284     }
File name: RPGMovement.cs Copy
22     void Start()
23     {
24         m_CharacterController = GetComponent();
25         m_Animator = GetComponent();
26         m_PhotonView = GetComponent();
27         m_TransformView = GetComponent();
28     }
File name: RPGMovement.cs Copy
30     void Update()
31     {
32         if( m_PhotonView.isMine == true )
33         {
34             ResetSpeedValues();
35
36             UpdateRotateMovement();
37
38             UpdateForwardMovement();
39             UpdateBackwardMovement();
40             UpdateStrafeMovement();
41
42             MoveCharacterController();
43             ApplyGravityToCharacterController();
44
45             ApplySynchronizedValues();
46         }
47
48         UpdateAnimation();
49     }
File name: RPGMovement.cs Copy
96     void ApplyGravityToCharacterController()
97     {
98         m_CharacterController.Move( transform.up * Time.deltaTime * -9.81f );
99     }
File name: RPGMovement.cs Copy
101     void MoveCharacterController()
102     {
103         m_CharacterController.Move( m_CurrentMovement * Time.deltaTime );
104     }
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     }

CharacterController 145 lượt xem

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