Projectile collision

Options

Hello,

I'm making a 2D 1v1 game, with ranged combat. The players throw objects (bananas) at each other.

I Instantiate the banana in a place, where it collides with the thrower. If I would Instantiate it further, it would look weird.

I have no problem with turning off the collision in the client of the thrower. I do it this way:

This method is in a script connected to the player prefab. So the throwerCollider is synced this way:

As I've said, On the screen of the thrower, the banana does not collide as intended. But in the second client it does get stuck.

I'm not sure if the solution is achievable with layers, because, I don't want the banana to hit the thrower, but I want it to hit the other player (player object is one of 4 prefabs).

I tried to send a RPC, that would turn off the collision in the second client (didn't work). Like this:

(Maybe I can't send the throwerCollider reference like this, not sure)

Tried to resolve it with the OnCollisionEnter2D, but I've probably set it up incorrectly.


If anything is unclear let me know. Any help or idea is appreciated!

Thanks for your time.

Answers

  • Tobias
    Tobias admin
    edited January 2022
    Options

    I'm not sure if the solution is achievable with layers, because, I don't want the banana to hit the thrower, but I want it to hit the other player (player object is one of 4 prefabs).

    There will be some way to do this but I actually can't help with this. It's more of a Unity question, too.

    I fear there will be some imprecision with colliding projectiles in PUN. Each player has authority of a part of the state (including the thrown banana). The more players there are, the more often their individual connections may delay the rpcs (outgoing so everyone receives it late or incoming so only one player gets a message delayed).

    All players can throw something at the same time and nobody can predict the other's projectiles until the RPC arrived. But when it does, it's late already.

    So there will be a lot of disagreement over this type of interaction. You need to choose who has authority of these collisions, so that client can update the others about collisions (or passing).

    I'm sorry to say but we don't intend to solve this in a general way for PUN.


    Fusion would be a lot better in this case, as one client is the Host and authoritative about the state. It must know when someone shoots and can detect collisions. It will automatically send the results of this as state to the others. Fusion has a similar pricing as PUN.

    Quantum would be on top of this situation entirely, as your simulation would have to be fully deterministic. Input is shared and once it's complete for a frame, this frame gets you the same results on all clients. You could even jump on bananas, if you wanted to. Quantum is way different than PUN and there is a monthly access fee for teams, so this is not necessarily a fit.

  • Kruzby
    Kruzby
    edited January 2022
    Options

    Thanks for your answer @Tobias !

    I'm just a student who's trying to learn something, that's close to him. So the Quantum option is out of reach. On the other hand I'll definitely check out Fusion. Thanks for the recommendation!

    Plus I want to share my little achievement, I managed to solve the problem on the Unity level. I Made few additions in the BananaItem script, which is attached to the BananaItem prefab.

    At first I disable collider of the projectile (banana):

    Then I use the StartCoroutine method:

    And this is the routine:

    As a result, collider of the banana is disabled for the first second of its existence (modifiable according to needs). It doesn't collide with the thrower on neither client. But can hit the enemy (after one second).

  • Tobias
    Tobias admin
    edited January 2022
    Options

    Thanks for sharing your solution to this problem.

    There must be some other way to setup the colliders because now, you might stand next to an enemy and due to the hardcoded delay, you won't be able to hit her/him.

    Great you are taking a look at Fusion!