通过角色控制器的碰撞不起作用

Collision via Character Controller doesn't work

我使用此代码来检测侧面碰撞,但它不起作用。我的播放器上有角色控制器,蓝色盒子上有盒子碰撞器,但当我与它们碰撞时它没有检测到碰撞。 https://i.stack.imgur.com/eUpOg.png

void OnControllerColliderHit (ControllerColliderHit hit){

    if (controller.collisionFlags == CollisionFlags.Sides) {

        Debug.Log (hit.gameObject.name);
        Debug.DrawRay (hit.point, hit.normal, Color.red, 2f);
    }

根据文档,OnControllerColliderHit will only be called while Move is being performed. That move must be initiated by the CharacterController's Move 函数而不是直接修改其 transform.position 属性。

public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
CharacterController controller;

void Start()
{
    controller = GetComponent<CharacterController>();
}

void Update()
{
    if (controller.isGrounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        if (Input.GetButton("Jump"))
            moveDirection.y = jumpSpeed;

    }
    moveDirection.y -= gravity * Time.deltaTime;
    controller.Move(moveDirection * Time.deltaTime); //This is how you move
}

void OnControllerColliderHit(ControllerColliderHit hit)
{

    if (controller.collisionFlags == CollisionFlags.Sides)
    {

        Debug.Log(hit.gameObject.name);
        Debug.DrawRay(hit.point, hit.normal, Color.red, 2f);
    }
}