Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question avoid that a player can jump on other player

Discussion in 'Physics' started by TheCrowXD, Jul 30, 2023.

  1. TheCrowXD

    TheCrowXD

    Joined:
    Jan 24, 2014
    Posts:
    5
    im making a multiplayer game and every player / enemy have a capsule collider and a character controller.

    the problem is that a player can jump on other player or a player is fighting an enemy and he can jump on it.

    note: make a smaller jump or a very tall collider arent viable solutions

    is there any way to force the player to get down or make him to slip or how other games fix this issue?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.     void OnControllerColliderHit(ControllerColliderHit hit)
    2.     {
    3.         if (hit.gameObject.tag=="Online Player") // have we touched another player?
    4.         {
    5.            velocity=Random.onUnitSphere*5f; // move/jump in some random direction.
    6.         }
    7.     }
     
  3. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    222
    Change the player objects layer to Player and set it to ignore itself.
     
  4. TheCrowXD

    TheCrowXD

    Joined:
    Jan 24, 2014
    Posts:
    5
    and what about the enemies? i dont want that the player jumps on an enemy and if the player ignores the enemies collider then he could go through them
     
  5. TheCrowXD

    TheCrowXD

    Joined:
    Jan 24, 2014
    Posts:
    5
    but then a player could keep pushing the other players just touching them. in fact 2 friendly players could never stay close with this code.
    i would need that it only happens when a player is over other player.
    i could add a second collider in every player like a "hat" and add this code to that collider but i want to see other options
     
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    It was just a little demo showing you how to detect contact with another player and then respond. It shouldn't be difficult for you to figure out how to only push the player if they're above the other player's head. Just compare the y values of their positions.