返回介绍

CharacterController.OnControllerColliderHit(ControllerColliderHit) 当控制器与碰撞器碰撞

发布于 2019-12-18 15:37:30 字数 2072 浏览 1577 评论 0 收藏 0

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

这个可以被用于推动物体,当它们碰撞角色时。

JavaScript:

// 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;
}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : 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;
    }
}

CharacterController

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文