x


OnCollisionEnter problem

So I have code that used to work with OnTriggerEnter to cause damage to other players. Now I put the trigger off my projectile and I want to use it to cause damage to people as well as hit walls and break them down, thus the no trigger.

Now I have the ability to collide with walls and trees to make them fall. However, I lost the ability to cause damage to players. Here is my code, it just doesn't work. I tried a bunch of different things and now I have no clue.

    void OnCollisionEnter(Collision current)
{   
    if (current.collider.tag == "Head") 
    {           
           current.gameObject.SendMessage("takeDmg", dmg);

        //current.SendMessage("takeDmg", dmg); Old code used to work with triggers.

        isActive = false;
    }
}

// Inside the Player class

void takeDmg(int dmg)
{   
// I know for a fact I get inside this method. Now I never make it pass the if below, because I think the OnCollisionEnter is sending me a current that doesn't really point to this object, or something along those lines.
    if (networkView.isMine)
    {           
        health -= dmg;      
    }
}

If I disable the networkView.isMine then neither player gets hit, or more weird things happen.

Please help.

more ▼

asked Feb 06, 2012 at 06:09 AM

Cocotimba gravatar image

Cocotimba
46 33 22 24

I tried, current.gameObject.SendMessage("takeDmg", dmg); current.collider.SendMessage("takeDmg", dmg);

Feb 06, 2012 at 06:11 AM Cocotimba

Neither works.

Feb 06, 2012 at 06:11 AM Cocotimba
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

take a look at the collider matrix. It shows you exactly when OnTrigger and OnCollision events get fired, and when they don't. You will most likely have to rearrange how your collisions and triggers work in combination with their components. Scroll to the bottom of this link.

http://unity3d.com/support/documentation/Components/class-BoxCollider.html

more ▼

answered Feb 06, 2012 at 09:46 PM

dannyskim gravatar image

dannyskim
4.5k 15 16 33

I dont think that fixes anything. The problem is with the code above when using things like current.gameObject.SendMessage

Feb 06, 2012 at 10:45 PM Cocotimba

If your old current.SendMessage worked, then what explains it not sending anymore? My point is, if the code worked before, and then you suddenly move from using OnTrigger to OnCollision and stuff suddenly breaks, the likelihood of the OnCollision not being called because of how you set up your colliders is most likely the culprit.

Feb 06, 2012 at 10:50 PM dannyskim

The code did change though. You cant use the same current.SendMessage("takeDmg", dmg); when using OnCollisionEnter. Now it looks something like: current.collider.SendMessage("takeDmg", dmg);

Feb 06, 2012 at 11:06 PM Cocotimba

Above I wrote that, I know for a fact I get inside this method (the takeDmg method). Now I never make it pass the if below, because I think the OnCollisionEnter is sending me a current that doesn't really point to this object, or something along those lines.

Feb 06, 2012 at 11:07 PM Cocotimba
(comments are locked)
10|3000 characters needed characters left

I think your network stuff was wonky, even before the changes. They are just showing it up.

SendMessage doesn't(?) do anything over the Network. If Steve shoots Alice, then Alice's copy of the bullet echo's Steve's "real" copy (you're using Network.Instantiate?) The real bullet on Steve's machine hits Alice first, due to lag, and turns itself inactive. It does nothing to the local copy of Alice, because of the isMine. Meanwhile, the bullet on Alice's machine, which was about to also hit Alice, freezes up -- it copies the (now inactive) real bullet.

I believe the "Unity recommended" way is to have the isMine check in the bullet. This solves the problem of one bullet thinking it hit, but the copy just barely missing. The "real" bullet sends an RPC("getHit") to all copies of what it hit, and another RPC (or Network.Destroy) to all of its copies.

more ▼

answered Feb 07, 2012 at 03:44 AM

Owen Reynolds gravatar image

Owen Reynolds
21.8k 2 8 72

Yes I am using Network.Instantiate. I think you are right and it should fix it. I’ll let you know.

Feb 08, 2012 at 05:20 AM Cocotimba

It didn't work. The health never went down though it did collide and the health -= damage works. Something odd with network view is happening. Is this right for showing the health as a text for the player?

if (networkView.isMine) { showHealth(); // the actual code that does gui works. }

If i remove that networkView.isMine for displaying HP for the player I can then see that damage is taken BUT then I display both my HP and the Enemie's.

Feb 08, 2012 at 06:53 AM Cocotimba

The multi-client setup has each client Net.Instantiate their player. In that case, isMine is just another way of saying "if this player is me" and should work fine.

I'd just have everyone print everything at first, and test the pants off the network stuff, make sure the RPCs are getting sent, get a feel for "my copy of his bullet,"... . Just make sure you can see the whole chain of events that lead to HP going down (and there's his HP and my copy of his HP. Arrgg!) and be sure you're trying to fix the correct part of it.

Feb 08, 2012 at 03:16 PM Owen Reynolds

Yeah I have the whole thing rigged with Debug.Log(s). Right now I am blaming the network...

Feb 08, 2012 at 08:17 PM Cocotimba
(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:

x345
x201
x23

asked: Feb 06, 2012 at 06:09 AM

Seen: 1846 times

Last Updated: Feb 08, 2012 at 08:17 PM