unity3d--CharacterController

来源:互联网 发布:域名网 编辑:程序博客网 时间:2024/05/22 19:04

转载自http://blog.csdn.net/kfqcome/article/details/17957997

背景

在使用摄像机游览场景的时候,需要控制摄像机不穿透场景中的物体,这需要用到碰撞。在unity物理引擎中有两类的情况可以检测到碰撞,一种是一方是刚体+碰撞器和另一方碰撞器碰撞(参加碰撞器和刚体),另一种就是Character Controller与其他的碰撞器碰撞的时候


在使用摄像机游览场景的时候,虽然需要去碰撞检测,但是一个特别的要求就是摄像机和其他物体碰撞之后不能对它们产生任何力的作用,同时摄像机本身也不需要受到模拟真实碰撞的反作用力,也就是说这个时候给摄像机挂载刚体属性还是不大适合的


这个时候Character Controller就被制作出来满足这种需求,Character Controller本身自带一个碰撞器,无需刚体即可完成触发(Trigger)和碰撞(Collision)功能。


使用

使用character controller来控制摄像机的移动,可以获取更加丰富的碰撞信息,完成更好的控制。


要使用角色控制器,首先将Character Controller 组件挂载到目标对象上,一般是摄像机的父节点,然后同时添加一个脚本,脚本获取Character Controller组件实例,并且在Update或者FixedUpdate函数中调用controller.Move更新控制器的位置(同时也会移动子节点摄像机的位置),假如与其他的碰撞器发生碰撞之后,角色控制器会被阻挡前进,同时会触发OnControllerColliderHit 函数。需要注意的是

OnControllerColliderHit要被触发,需要在同一脚本中同时要调用角色控制器的Move函数。character controller与其他的碰撞器发生碰撞后,会自动调用函数

void OnControllerColliderHit (ControllerColliderHit hit)


ControllerColliderHit

ControllerColliderHit is used by CharacterController.OnControllerColliderHit to give detailed information about the collision and how to deal with it.

Variables

collider

The collider that was hit by the controller.

controller

The controller that hit the collider.

gameObject

The game object that was hit by the controller.

moveDirection

Approximately the direction from the center of the capsule to the point we touch.

moveLength

How far the character has travelled until it hit the collider.

normal

The normal of the surface we collided with in world space.

point

The impact point in world space.

rigidbody

The rigidbody that was hit by the controller.

transform

The transform that was hit by the controller.


示例

创建一个空游戏对象FpsController,给它添加CharacterController组件,同时添加一个摄像机作为它的子成员,同时将如下代码挂到FpsController上

[csharp] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. [RequireComponent (typeof(CharacterController))]  
  2. public class FPSNavigator : MonoBehaviour   
  3. {  
  4.   
  5.   
  6.     public KeyCode mKeyLeft = KeyCode.LeftArrow;  
  7.     public KeyCode mKeyRight = KeyCode.RightArrow;  
  8.     public KeyCode mKeyForward = KeyCode.UpArrow;  
  9.     public KeyCode mKeyBackward = KeyCode.DownArrow;  
  10.       
  11.     public float mKeyStrokeMoveStep = 0.07f;    //metre  
  12.   
  13.     private CharacterController controller;  
  14.     private Vector3 mMoveDir ;  
  15.       
  16.     void Start () {  
  17.       
  18.         controller = GetComponent<CharacterController>();  
  19.   
  20.     }  
  21.       
  22.     // Update is called once per frame  
  23.     void Update () {  
  24.       
  25.         Vector3 vDir = Vector3.zero;  
  26.         if(Input.GetKey(mKeyLeft))  
  27.         {  
  28.             vDir.x -= mKeyStrokeMoveStep;  
  29.         }  
  30.         if(Input.GetKey(mKeyRight))  
  31.         {  
  32.             vDir.x += mKeyStrokeMoveStep;  
  33.         }  
  34.   
  35.         if(Input.GetKey(mKeyForward))  
  36.         {  
  37.             vDir.z += mKeyStrokeMoveStep;  
  38.         }  
  39.         if(Input.GetKey(mKeyBackward))  
  40.         {  
  41.             vDir.z -= mKeyStrokeMoveStep;  
  42.         }  
  43.         mMoveDir = transform.rotation*vDir;  
  44.   
  45.         controller.Move(mMoveDir);  
  46.       
  47.     }  
  48. }  
1 0
原创粉丝点击