x


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!

more ▼

asked Jul 07 '11 at 07:52 PM

Henri 1 gravatar image

Henri 1
94 18 19 22

Does the player have a collider?

Jul 07 '11 at 08:14 PM Dreamblur

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 :(

Jul 08 '11 at 09:16 AM Henri 1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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

more ▼

answered Jul 08 '11 at 10:18 AM

Grady gravatar image

Grady
1.1k 66 70 82

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?

Jul 09 '11 at 02:20 PM Henri 1

Hi @Grady,

I renamed my script and it seems to eliminate 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 :(

Jul 09 '11 at 02:38 PM Henri 1

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

Jul 11 '11 at 08:24 AM 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. Maybe you can check it out? Adios

Dec 13 '11 at 09:21 PM Henri 1

OK, glad I could be of some help!

Dec 14 '11 at 11:12 AM Grady
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2505
x1797
x1707
x227
x185

asked: Jul 07 '11 at 07:52 PM

Seen: 2312 times

Last Updated: Dec 14 '11 at 11:12 AM