OnCollisionEnter problems

My apologies for asking such a simple question.

I have this code attached to my character (FPS character controller):

void OnCollisionEnter(Collision c) {
  print("hello");
}

I’ve attached a RigidBody to both the character controller and a cube in my level. However the code is never triggered when I’m touching or standing on the cube.

I believe that you would only need a RigidBody on one. Try that.

The answer you've accepted below is incorrect...

2 Answers

2

void OnCollisionEnter(Collision c) {
print(“hello”);
}

The problem is that you don’t have an if statement. You may also need to give a tag to the cube. Try this:

void OnCollisionEnter(Collider) {
if (other.tag "Put what you want here") {
print("hello");
}
    }

The content of a function has no effect on whether it's called.

You may want to use OnControllerColliderHit instead. I believe CharacterControllers don’t really work like other physics objects; I’m not entirely sure if they can be used as such. See this page: unitygems.com