Collision detection (533100)

Hello all, I am trying out a very elementary thing in unity..basically I have a cube and a player character.

Whenever the player collides with the cube, I want to print something like “3D Collision with another object named: Cube”

Now I have only a character controller attached with my player, and a Mesh Collider, Box Collider and Mesh Rnederer attached with my cube. The screenshot of the inspector panel of the cube is attached.

I searched online and came up with the following script:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	 
	// Use this for initialization
	void Start () {
		
	
	}
	
	
	// Update is called once per frame
	void Update () {
	
	}
	void OnCollisionEnter(Collision collision)
    {
       
		if (collision.gameObject.name == "Cube")
		{
			Debug.Log("3D Collision with another object named: "+collision.gameObject.name);
	        }  
     }
	
	
	
}

Whenever I collide with the cube, nothing is displayed :frowning:

I can’t understand why..

I remember that the CharacterController acts a little differently than a normal rigidbody when colliding with things. If I remember correctly, the CharacterController.Move() command does not interact with rigidbodies. Ie: the CharacterController moves AROUND colliders, but does not send collision messages.

There is OnControllerColliderHit() that gets called only on the CharacterController that you can use to detect Move()ing into a collider and take some action from there.

I might be missing some details… but the important thing is that collision events act a little different for CharacterControllers!

EDIT : I was going to suggest to attach a rigidbody but it turns out you already did. Do what the poster above me suggested. :roll_eyes:

RigidBody component is already attached to the cube.

I tried something like:

void OnControllerColliderHit(ControllerColliderHit hit) {
		if (hit.gameObject.name == "Cube")
		{
			Debug.Log("3D Collision with another object named: "+hit.gameObject.name);
	} 
}

but still couldn’t get it to work…Do I have to attach any Component to my player?

Can you put a debug statement before the “if” statements? I’m wondering if the OnControllerColliderHit and OnCollisionEnter functions are even being called.

Secondly, how are you moving the controller? What is the code that moves the controller into the collider in the first place?

Okay, so I put a Debug.Log(“Howdy”); before te if statements and no it did not print in the console window.

Also, I’m simply using the inbuilt character controller inbuilt in the Unity engine. I just press the ‘Up’ key and collide with the cube…

Is the script with OnControllerColliderHit on the same object that has a character controller?

Also, I was expecting a script somewhere that detects the ‘Up’ key and tells the CharacterCollider to Move(). If it’s moving some other way, like not using the Move() or SimpleMove() command, then OnControllerColliderHit would not get called.

Yes it is.

I’ve just begun using unity engine, am just learning to code, so thought it to be better to use the default character controller package that comes with unity.
Is there a way out from this?

I almost want to say use colliders and rigidbodies and ignore the CharacterController for now because then you only need to deal with rigidbodies, not both rigidbodies and CharacterControllers…

I’m still not sure how your character is actually changing position. I don’t think Unity’s CharacterController will automatically listen for the up key and start moving around.