根据文档,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);
}
}