Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple Collision 2D (Video example)

Discussion in '2D' started by fosmark13, Sep 14, 2016.

  1. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    hi, i've been reading a lot of this but i can't find a solution to this problem. It consist that my player i having multiple collisions with many enemies at the same time and i have a knockback method that implies force when the player collides with the enemy, i actually use OnTriggerEnter2D to do this, after the player is hit it gets an invulnerability for a few seconds and then he can get knockback again for the enemies, the problem is that the enemies can be to close to each other that when the player touches them it triggers both colliders and the knockback force is added, for example, the knockback force is 200 on X and 100 in Y, when this happens and the player collides with 2 enemies at the same time it's 200 * 2 on X and 100 * 2 on Y, so he goes super fast and gets too far!!

    i have read some other post and forums and i already tried put a boolean saying if IsColliding and stuff, i tried to limit the velocity when is on knockback mode but it's just not the effect i'm looking for.

    So the cuestion is simple.. is there a way to avoid multiple collision (or colliding triggers) at the same time with many objects??

     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Setting a boolean or cooldown for knockback should work fine. What happened when you tried that way?
     
  3. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    nothing...the collision still happens this is how i'm doing it:

    Code (CSharp):
    1.  
    2.  
    3. void OnTriggerEnter2D(Collider2D other){
    4.  
    5.             if (other.CompareTag ("Player") && player.isDead == false) {
    6.  
    7.             if (!playerColliding) {
    8.              
    9.                 playerColliding = true;
    10.  
    11.                 player.Damage (1); /// this calls the Knockback method in the players script
    12.  
    13.                 player.move = 0;
    14.  
    15.                 player.GetComponent<Rigidbody2D> ().velocity = (new Vector2 (0, 0));
    16.  
    17.                 player.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (0, 0));
    18.  
    19.                 Debug.Log ("knockback by groundTracker");
    20.  
    21.                 player.canJump = false;
    22.  
    23.  
    24.  
    25.             }
    26.  
    27.         }
    28.  
    29.  
    30. //after in the Update i put this code to desactivate the playerColliding bool
    31.  
    32.  
    33. void Update () {
    34.  
    35. if (playerColliding) {
    36.  
    37. deactivationInProcess = true;
    38.  
    39. }
    40.  
    41. if(deactivationInProcess){
    42.  
    43. playerCollisionDeactivation += Time.deltaTime;
    44.  
    45. if (playerCollisionDeactivation >= 0.2f) {
    46.  
    47. playerColliding = false;
    48.  
    49. deactivationInProcess = false;
    50.  
    51. playerCollisionDeactivation = 0;
    52. }
    53.  
    54.  
    55. }
    56.  
    57.  
    58. }
    59.  
    60.     }
    i even tried to only set to true the boolean in the OnTriggerFunction and then in the update do all the stuff...but i have the same result... i think is that they are entering at the same time
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    It is not possible for the two functions to run at the same time. They can happen within the same frame, but one will always happen first.

    I'm a little confused with where these functions are written. Is the OnTriggerEnter2D within the enemy? If so, where is the "player" reference getting set, and how is "playerColliding" getting set? Do these variables exist on the enemy class? If so, you should know that each enemy has it's own version of these variables, each with a different value. Setting "playerColliding" on one enemy does not set it for all of them.

    Similarly, is the Update function on the enemy? That seems like the kind of thing the Player should be handling.

    I would think that the player should have the "isColliding" variable, and the enemies would call a function on the player like "Knockback(Vector3 force)", where the player would set "isColliding", and add the force. Then the player would keep track of time until setting "isColliding" back to zero.

    The enemies would simply check the "player.isColliding" value, and call the "player.Knockback()" function.
     
    Last edited: Sep 14, 2016
    PeaceLaced and fosmark13 like this.
  5. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91

    ok let me check it that way and i'll tell you the result, thanks a lot man
     
  6. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    @jeffreyschoch hi again, i tried what you said and put the update method inside the player's script and it worked!!! thanks a lot now the knockback method works as it suppose to work, thanks man!!!
     
    LiterallyJeff and PeaceLaced like this.