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
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!
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?
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.
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.