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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prevent rigidbodies pushing each other

Discussion in 'Scripting' started by RPGcakes, Nov 19, 2018.

  1. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    I'm having an issue in the game I'm making:

    My player character is being pushed around by enemies endlessly across the map and sometimes through Colliders.

    I've tried changing the mass/forces of the 2 characters and I've also tried dynamic/kinematic settings but I'm confused as to how to prevent this from happening

    I'm using a rigidbody 2d and a 2d box collider on both the player and the enemy.

    Any ideas?
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Layers control what can push what.
     
    RPGcakes likes this.
  3. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Thanks for your help!

    Well what I'm going for is for both the enemy+player to be unpushable by each other, but also not able to pass through each other, is this possible to set up via layers?

    Another issue I'm having is that upon colliding it's causing the camera to shake and distort the game world, is there a work-around for that?
     
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You want to just prevent movement in the direction of the collision then. This is a bit of code you would script that would either intercept contacts (advanced) or just simply cancel out movement in the direction of the thing it can't pass through.

    There's no built-in method to achieve this as it's not a common thing for a _physics_ engine. Usually 2D games that might do this have their own movement and collision code and don't rely on Box2D.
     
    RPGcakes likes this.
  5. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    Alright thanks, I'll look into cancelling out the movement based on direction, if it's too complicated I wouldn't mind the character+enemy pushing each other just as long as it was by a small amount rather than constantly sliding.
     
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    When things collide with anything, a quick and dirty way of solving it would be to do something like (untested) where rb is a reference to Rigidbody2D:

    rb.velocity = Vector3.ProjectOnPlane(rb.velocity, collisionNormal);

    This simply wipes out any movement toward normal but preserves everything else. It's what you might consider sliding collision.

    https://docs.unity3d.com/ScriptReference/Vector3.ProjectOnPlane.html
     
  7. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22

    Thanks again for the help.

    Would that be written like this and attached to the enemy controller?

    Code (CSharp):
    1. private void OnCollisionEnter(Collision collision)
    2.     {
    3.         myRigidbody.velocity = Vector3.ProjectOnPlane(myRigidbody.velocity, collision.contacts[0].normal);
    4.     }
     
  8. mattis89

    mattis89

    Joined:
    Jan 10, 2017
    Posts:
    1,151
    Did it work?