- 最后登录
- 2016-8-29
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 23585
- 纳金币
- 20645
- 精华
- 62
|
第一种函数:function OnControllerColliderHit (hit:ControllerColliderHit)
主角专用函数
例子:function OnControllerColliderHit(hit:ControllerColliderHit)
{
if (hit.gameObject.tag=="playerDoor" && doorisopen==false)
{
currentdoor=hit.gameObject;
opendoor(hit.gameObject);
}
}
不足之处:当碰撞物体碰撞模式为:(Is Trigger 触发模式) 时,主角与物体不会发生碰撞,而且会穿过物体。
第二种函数:static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
- origin
The starting point of the ray in world coordinates.
在世界坐标,射线的起始点。
- direction
The direction of the ray.
射线的方向。
- distance
The length of the ray
射线的长度。
- hitInfo
If true is returned, hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).
如果返回true,hitInfo将包含碰到器碰撞的更多信息。
- layerMask
A Layer mask that is used to selectively ignore colliders when casting a ray.
只选定Layermask层内的碰撞器,其它层内碰撞器忽略。
此碰撞函数可用于射击游戏里面发射枪弹。
应用例子:
RaycastHit info;
bool hit =Physics.Raycast(枪口的位置 , 摄像机的正前方, out info , 100 ,敌人碰撞层)
{
if(hit)
{
if(info.transform.Tag.CompareTo("enemy")==0)
{
}
//在射中的地方释放一个粒子效果
Instantiate(粒子,info.point, info.transform.rotation);
}
}
第三种函数:function OnTriggerEnter(hit:Collider)
优点:触发模式碰撞,脚本是运用于碰撞物体上,这样大大减轻了主角身上脚本数量的负担。(强烈推荐)
注意:不要忘记勾选Is Trigger
第四种函数:function OnCollisionEnter (collisionInfo : Collision)
相对于OnTriggerEnter,OnCollisionEnter传递的是Collision类而不是Collider。Collision包含接触点,碰撞速度等细节。如果在函数中不使用碰撞信息,省略collisionInfo参数以避免不必要的运算。注意如果碰撞体附加了一个非动力学刚体,只发送碰撞事件。
也就是说必须要有力才可以发生碰撞。(不推荐使用这种方法)
|
|