移動が行われている間にドキュメントによれば、OnControllerColliderHit
のみ呼び出されます。その移動は、transform.position
プロパティを直接変更するのではなく、CharacterControllerのMove
関数によって開始されなければなりません。
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);
}
}
「Debug.Log(hit.gameObject.name);」をif文の外側に置いて、ログを取得するかどうかを確認することをお勧めします。 – Programmer
私はやったと私はどちらかを取得しません。 – N1ckGreek
これは興味深いです。キューブにコライダーがあり、コライダーに 'IsTrigger'がディスエーブルまたはチェックされていないことを確認できますか?また、各ColliderにRigidbodyを追加し、何が起こるかを確認してください。 – Programmer