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

Classic Question: Platformer, stuck on wall while jumping

Discussion in 'Scripting' started by ChrisX, Mar 23, 2015.

  1. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Hello all!

    I know this is probably a very oft-asked question, but I really would like to know this one situation. I've managed to set up my 2D character movement well, except when it comes to walls. I jump there and if I keep pressing move to the wall, they're stuck there like moving on a floating invisible ground. I want it that if I press against the wall, I don't get stuck.

    A lot of stack overflow questions say that I should change the wall friction or something (or add a new field) in Project Settings. No luck, there's no 'wall friction' parameter there nor can I add new Parameter.

    A little help here? Thanks.
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    How did you set up your "grounded" part of the character controller?
    Is it possible that Unity thinks that hitting the wall is being grounded (ray cast?), and resets your current height as the new zero or something?
     
  3. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    This is how I set up the grounded part.

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D coll){
    2.         if (coll.collider.tag == "Ground") {
    3.             playerJump = PlayerStates.Jump_State.Grounded;
    4.             jumpLimit = 2;
    5.         }
    6.         if (coll.collider.tag == "Wall") {
    7.             print ("Hitting a wall");
    8.         }
    9.     }
     
  4. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well I am not seeing anything that should cause it to get stuck.
    Is it tile-based? Maybe the character is stuck between two tiles stacked one upon the other.
    Also, is it possible that since you do nothing when hitting a wall, the character somehow moves a few units inside it?
    Perhaps try that on collision with a wall, the x value does not change, or even detracts one.
     
  5. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    That was just to detect whether it hits a wall or not, but regardless, even if that is not put, it is still stuck.

    I don't think I'm advanced enough to use tile based. No.

    Uh... so you mean when colliding on a wall, we decrease the transform so it backed away a little from the wall? I dunno, it'll look awkward...
     
  6. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    It will, I wouldn't recommend it as a permanent code, but I'd like to know if that is the issue (going too far into the wall and getting stuck) or if it's actually the touching of the wall that causes the issue.
     
  7. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    I think I'll just give you my code and see where it's wrong. My analytical skill is a little lacking.

    I also once tried that transform decrease, but it just makes the problem become awkward.
     

    Attached Files:

  8. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Alright, I'll give it a look once I am off work (around 9 hours from now)
     
  9. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Okay, I'll wait. I'll just post here so you don't have to worry about double posting.
     
  10. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Sorry about the delay, I've received a heatstroke yesterday (excuses excuses!)
    I'll read it 100% today.
     
  11. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Oh my God. I hope you're okay.
     
  12. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Haha thanks for the concern.
    In Israel, yesterday was "Good Deeds Day", so my company went and hosted a game tournament for kids with issues at home for the entire day, plus breakfast and BBQ. (50 kids around ages 16+, it was actually fun, and they were well behaved!)
    Yesterday the weather started getting hot, so I guess it affected me.
    I apologize for the delay.
     
  13. Roderyk

    Roderyk

    Joined:
    Mar 5, 2015
    Posts:
    75
    Are you using Physics or directly changing the transform component of your game object?
     
    TheValar likes this.
  14. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I have Unity 5, which updated the project, and completely screwed with it.

    But the wall does not freeze the character for me, rather, it's the animator that keeps floating my character up.
    Can you if you disable the animations themselves, does it still give you issues?
     
  15. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Yeah the same thing happened to me in Unity 5 and the project was screwed with that, it kept floating up. I recommend getting Unity 4 for that.

    However, I found out that the problem solution is that you have to disable 'Apply Root Motion' on the animator and then the character doesn't freeze. The problem of sticking to the wall still remains though.

    Also in Unity 4, when I enabled Apply Root Motion, it does nothing. But here, it makes it float. Looks like a Unity 5 problem.

    @Roderyck: I'm using Physics, of course. You know, rigidbody and velocity.
     
    Last edited: Mar 25, 2015
  16. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I see, well, I'll download Unity 4 and try it out then.
     
  17. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Uh actually you could try that in Unity 5. Click on the Player and then uncheck the 'Apply Root Motion'.
     
  18. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Ah as for that part, I did it already, without it, the character started falling, through the wall and ground.
    I'd rather try the entire thing with Unity 4.
    I do hope someone solves it before I do, It's a pain downloading a new program :p
    Maybe I should keep Unity 4 on the side...
     
  19. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Well, that is weird, because when I do that, I don't fall through the wall or ground...