Not hitting yourself

Options
Hi there,

First I made the player spawn their own spells (2D fireball).
I made it work but it wasn't as I desired, a lot of issues with who decided when someone else has been hit
So now I have an invisible player (not really an invisible player just a connection) casting all the spells etc.

This is in the player class, will be triggerd onClick but thats not important.
if (attackType == "range") { nextAttack = Time.time + attackRate; PhotonNetwork.RPC(PV, "shoot", PhotonNetwork.masterClient, true, transform.position, attackDirection()); }

// also in the player class
[PunRPC] private void shoot(Vector3 pos, Quaternion dir) { // the only call to this function should go to the master but ok... You Never Know :) if (PhotonNetwork.isMasterClient) { PhotonNetwork.Instantiate(attackPreFab.name, pos, dir, 0); } }

// onCollision in the 'attackPreFab' it be a fireball or an arrow, doesn't matter.
void OnTriggerEnter2D(Collider2D coll) { if (PhotonNetwork.isMasterClient) { if (coll.gameObject.GetComponent<PhotonView>() != null) { if (coll.gameObject.GetComponent<PhotonView>().isMine == true && isImmune) { return; } } Explode(); } }

So ofcourse the "if (coll.gameObject.GetComponent().isMine" isn't work anymore as the caster is no longer the owner. This will mean that he will be hit as soon he casts the spell.
Now I could think of the quick solutions but both... Well have downsides.
First of all, I could just use an immune timer (already have one because only the first half second the player shouldn't be hit after that the spell can be reflected).
But this would mean that if you try to hit someone who is right next to you he wouldn't be hit.

The second option is to use a distance, so while distance is smaller than x don't do damage.
But the same downside applies, or I have to make it really precise what wouldn't work because the player could be moving so...

Anyone who has an awesome idea how to deal with this situation?

Best regards,
David

Comments

  • Hi David,

    is there any reason why a non-MasterClient player is not allowed to cast spells (means calling PhotonNetwork.Instantiate, actually he can cast spells on his own) and need to 'ask' or 'command' the MasterClient first? I guess the better way is to allow any client to call PhotonNetwork.Instantiate if he casts a spell. This would also make if (coll.gameObject.GetComponent<PhotonView>().isMine == true && isImmune) valid and functional.

    You additionally told us that a spell can be reflected. This can be done by the player who is reflecting the spell and a RequestOwnership function call. You can read more about ownership transfer in the documentation.
  • Hi Chris,

    thank you for your reply!
    When a player are lagging it is always 'unfair' to let either one of them decided if the other player was hit.
    I have tested it a few hours with some friends and we found out that it happend a lot that the caster thought he did hit the target but that the target just didn't agree.

    An other thing that really annoyed us was that if we did let the caster 'decided' everything that often we would destroy the object before it did hit anything on the side of the other player.

    Even when taking over the controll, on the caster side it will look like he did already hit the target.
    While on the target side he did a perfect reflect...

    I guess that PhotonNetwork.Instantiate is acting like PhotonTargets.AllBuffered in an RPC method.
    While we are looking for something more like PhotonTargets.AllBufferedViaServer.

    Best regards,

    David
  • Hi David,

    I guess that PhotonNetwork.Instantiate is acting like PhotonTargets.AllBuffered in an RPC method.


    Somehow yes. Instantiation on the local client is done on calling the function. Remote players will receive this call across the network. So you actually have some delay between instantiation on one client and the others.

    if you don't have any movement synchronization on the instantiated object yet, you can give that a try by observing the object's transform by the PhotonView component. This way the current position is send to other clients 20 times per second (default I guess) so that this might give a better visual result.