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

Question How to stop my characters from sliding on the floor?

Discussion in 'Physics' started by AnduinGaiden, May 17, 2020.

  1. AnduinGaiden

    AnduinGaiden

    Joined:
    Feb 13, 2015
    Posts:
    9
    Hi there,

    I have a scene with multiple characters (1 original GameObject and other copies of it) and I was implementing a script to allow me to change the controls from one character to another whenever I press a specific key.

    It turns out that I noticed a strange behavior that only happens when I have more than 1 active character in the scene. If I have a single active character Gameobject active, it behaves well, no strange left sliding happens.

    As soon as I enable (SetActive=true) the other character Gameobject, both of them start to slide to the left, and if one character touches the other it slides even faster in some direction.

    Both characters have a Physical2D material applied to them with 0 bounciness and friction. I did it in order to avoid having them sticking on the walls.

    How can I do to stop this from happening and at the same time be able to have my chars not "gluing" on the wall when jumping and colliding with it?

    (Obs.: I already tried to apply the Physical2D material to the floor/ walls...and removed it from the chars, but it did not work also)

    Thanks in advance!

     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,778
    Using zero friction like that is a hack and is often used when the movement controller isn't aware that its movement is causing large forces against walls; often causing the solver to focus on that force or it can simply be the movement method i.e. using MovePosition continuously.

    A good method is to use Rigidbody2D.IsTouching with an appropriate ContactFilter2D to determine direction of the collision normal. You can even do a few of these for checking grounded below or contact with walls left/right etc. I have a GitHub repo here and a scene that demonstrates that here where the important part is the detection of being "grounded" comes down to this script. IsTouching just looks at existing contacts so is pretty fast. Usnig it to determine if you're currently touching the "ground" left or right and simply not allowing your controller to send you that way will stop this.

    The fact though that you see a difference when you have multiple characters can only mean they are (somehow) linked script or are somehow different. I can think of no other reason right now.
     
    AnduinGaiden likes this.
  3. AnduinGaiden

    AnduinGaiden

    Joined:
    Feb 13, 2015
    Posts:
    9
    Hey @MelvMay, first of all, thank you very much for your prompt response.
    Unfortunately, your solution did not work for me. I will need to do some more research about it. It is driving me crazy since I am stuck with this issue for more than 1 week. Almost giving up on this prototype idea. :(
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,778
    It works perfecty fine in the demo I linked so maybe there's something else going on. If you want to host the project to a cut-down test project then I'd be happy to take a look at it for you.

    I can also create a secure workspace for you to upload to if you like. Just DM me your email if you need that.