Unity3D圣典3.3版本-中文版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
CharacterController.OnControllerColliderHit 控制碰撞器碰撞
# [CharacterController](CharacterController.html).OnControllerColliderHit 控制碰撞器碰撞 function *OnControllerColliderHit* (*hit* : [ControllerColliderHit](../ControllerColliderHit/ControllerColliderHit.html)) : void *Description* 描述 OnControllerColliderHit is called when the controller hits a collider while performing a Move. 当控制器碰撞一个正在运动的碰撞器时,OnControllerColliderHit 被调用。 This can be used to push objects when they collide with the character 这个可以被用于推动物体,当他们碰撞角色时。 * [C#](#) * [JavaScript](#) ``` using UnityEngine; using System.Collections; public class example : MonoBehaviour { public float pushPower = 2.0F; void OnControllerColliderHit(ControllerColliderHit hit) { Rigidbody body = hit.collider.attachedRigidbody; if (body == null || body.isKinematic) return; if (hit.moveDirection.y < -0.3F) return; Vector3 pushDir = new Vector3(hit.moveDirection.x, 0, hit.moveDirection.z); body.velocity = pushDir * pushPower; } } ``` ``` // this script pushes all rigidbodies that the character touches // 这个脚本推动所有的角色碰撞到的刚体。 var pushPower = 2.0; function OnControllerColliderHit (hit : ControllerColliderHit) { var body : Rigidbody = hit.collider.attachedRigidbody; // no rigidbody // 没有刚体。 if (body == null || body.isKinematic) return; // We dont want to push objects below us // 我们不想推动在我们下边的物体。 if (hit.moveDirection.y < -0.3) return; // Calculate push direction from move direction, // we only push objects to the sides never up and down // 通过移动方向计算推动方向,我们只把物体推到两侧,从不向上和向下推。 var pushDir : Vector3 = Vector3(hit.moveDirection.x, 0, hit.moveDirection.z); // If you know how fast your character is trying to move, // then you can also multiply the push velocity by that. // 如果你知道你的角色移动的有多快,那么你也可以用它乘以推动速度。 // Apply the push // 应用推力。 body.velocity = pushDir * pushPower; } ```
da
2022年5月17日 17:29
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码