function OnControllerColliderHit (hit : ControllerColliderHit) : void
Description描述
OnControllerColliderHit is called when the controller hits a collider while performing a Move.
在移动的时,当controller碰撞到collider时OnControllerColliderHit被调用。
This can be used to push objects when they collide with the character.
它可以用来在角色碰到物体时推开物体。
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 : float = 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;
}