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