0

我使用此代码来检测侧面的碰撞,但它不起作用。我将角色控制器连接到我的播放器和蓝色盒子上的盒子碰撞器,但是当我与它们碰撞时它没有检测到碰撞。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);
    }
4

1 回答 1

0

根据文档,OnControllerColliderHit 只会在执行 Move 时调用。该移动必须由 CharacterController 的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);
    }
}
于 2016-10-23T15:55:02.517 回答