- Home /
Cannot get collision to do anything
Good day fellow Uniters!
Haven't used Unity since April, so I'm again confusing myself.
For some reason, I cannot get Collision to work :?
Script attached to my Cube (Which the player needs to walk into in order to test my Awesomely simple health script) as follow:
function OnCollisionEnter(collision : Collision) {
if(collision.rigidbody){
gameObject.FindWithTag("Player").SendMessage("ApplyDamage");
print ("Hit!");
}
And on the receiving side (Player) the following:
function ApplyDamage () {
Debug.Log("Hit!");
}
And VIOLA! Nothing happens :(
I have RigidBodies attached to both the Cube and the Player, Cube also has a Box Collider.
If any Einstein replica's are willing to ease my pain, I bake quite lekker cookies (Shipping Excluded) :)
Thanks!
Hi Dreamblur, the player has no specific collider (such as mesh or box collider) but it does have a ThirdPersonController script which basically has a capsule collider. Everything in my scene works 100%, player can walk, jump, fly, run shoot beams, powerup etc, just the darn collision doesnt work. If you browse my previous questions, you will notice that I have struggled before with sending messages, but at that time my collision worked 100%. Now I deleted the script, cant find it in my backups and couldn't remember exactly what I need to do :(
Answer by Grady · Jul 08, 2011 at 10:18 AM
make sure you have a collider on both, and that one has a non kinematic rigidbody....
If that still doesn't work, then try this script
function OnCollisionEnter(collision : Collision){
if (collision.gameObject.tag == "Player"){
gameObject.FindWithTag("Player").SendMessage("ApplyDamage");
print("Hit!");
}
}
alternatively, you can try this:
var collisionObject : GameObject;
function OnCollisionEnter(collision : Collision){
if (collision.gameObject == collisionObject){
gameObject.FindWithTag("Player").SendMessage("ApplyDamage");
print("Hit!");
}
}
Hope this helps....
If you need more help, then comment back...
-Grady
Hi @Grady.
I keep getting the error message "Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored."
No matter what I try... Previously I have not received this error and no matter what I do, as soon as I click play, the error occurs. I do not have any other collision functions in my scene... Any Ideas?
Hi @Grady,
I renamed my script and it seems to eli$$anonymous$$ate the error, but it still wont collide... I created a new scene, added some scripts and the collision did register, but it seems as if something in the script aint right, as it does register collision, but not with the script specified (collision.gameObject.tag == "Player"). I did try multiple other ways, but still not working :(
make sure that both objects have colliders, one has rigidbody that is non kinematic, and make sure that the object that you are attaching this script to is not tagged "Player". The object that you want to collide with has to be tagged "Player".
So in other words, the other object that does not have this script on it must be tagged "Player"
I've also edited my answer above so you can try a different method...
-Grady
Hi @Grady,
So long since I replied. Sorry that I havent chose your answer as "Best Answer", but I ended up using "OnControllerColliderHit" which worked, but for some reason slowed down my audio playback everytime I use one of my player's powers. Thanks again for your help. Again I'm strugling with my collisions, just posted a question. $$anonymous$$aybe you can check it out? Adios