Search Unity

2D game movement issues - Stuck On Walls

Discussion in 'Scripting' started by squigglyo, May 13, 2013.

  1. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    Hey there Unity Developers

    I have some issues with my 2D games movements. The movement works fine, but when walking into a wall, the character gets vertically stuck.

    The character stops on the wall, but if they are midair at the time, they stay at the point of collision for as long as the directional key is held.

    Here is the movement script, which goes on a gameobject with a CubeCollider and a Rigidbody.
    The floor objects are simply Cube Colliders.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class NewControls : MonoBehaviour {
    6.     public bool grounded = false;
    7.     public float jumpHeight = 10f;
    8.     public int moveDir = 0;
    9.     public float moveSpeed = 4f;
    10.    
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.     public void JumpButton(bool touched)
    16.     {
    17.         if(touched == false)
    18.             return;
    19.        
    20.         if (grounded) {
    21.             rigidbody.velocity = new Vector3 (0f, jumpHeight, 0f);
    22.             grounded = false;
    23.         }
    24.     }
    25.    
    26.     public void MoveButton(bool touched, DIRECTION dir)
    27.     {
    28.         if(!touched)
    29.         {
    30.             moveDir = 0;
    31.             return;
    32.         }
    33.        
    34.         if (dir == DIRECTION.LEFT) {
    35.    
    36.             moveDir = -1;
    37.        
    38.            
    39.         } else if (dir == DIRECTION.RIGHT) {
    40.             moveDir = 1;
    41.        
    42.         }
    43.     }
    44.    
    45.     void Update ()
    46.     {
    47.         if (Input.GetKeyDown ("z")) {
    48.             JumpButton(true);
    49.         }
    50.            
    51.                                
    52.         if (Input.GetKey ("left")) {
    53.             MoveButton(true, DIRECTION.LEFT);
    54.         } else if (Input.GetKey ("right")) {
    55.             MoveButton(true, DIRECTION.RIGHT);
    56.         } else if(moveDir != 0){
    57.             MoveButton(false, DIRECTION.LEFT);
    58.         }
    59.            
    60.     }
    61.    
    62.     void FixedUpdate ()
    63.     {
    64.          float movement;
    65.    
    66.            
    67.            
    68.        
    69.        
    70.         movement = moveDir * moveSpeed;
    71.         movement *= 0.75f;
    72.         movement *= Time.deltaTime;
    73.         //RaycastHit hit;
    74.         //if(!rigidbody.SweepTest(new Vector3(moveDir,0,0), out hit, gameObject.transform.localScale.x / 10))
    75.         transform.Translate(movement, 0, 0);   
    76.        
    77.  
    78.         // raycast check to see if the player is standing on the ground
    79.        
    80.         if (Physics.Raycast (transform.position, Vector3.down, 0.5f)) {
    81.             if (!grounded  rigidbody.velocity.y < jumpHeight / 10)
    82.             {              
    83.                 grounded = true;
    84.             }
    85.         }else {
    86.             grounded = false;
    87.         }
    88.        
    89.        
    90.     }
    91. }
    92.  
     
  2. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,552
    Add a zero friction physics material to your wall colliders. The character should slide down as expected.
     
  3. Partel-Lang

    Partel-Lang

    Joined:
    Jan 2, 2013
    Posts:
    2,552
    ...or if you need your walls to be walkable you can dynamically set your character's physics materal to 0 friction while ungrounded
     
  4. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    I added a new Physics Material to the walls and set dynamic and static friction to 0.
    This didnt seem to affect anything but I have kept it on just in case.

    I added a Physics Material to the player and in the code, when he is midair, he has no dynamic or static friction, and when he lands, he does. This works for the most part now, he slides down walls.

    but, when he gets to the bottom of the wall, if there is a gap inbetween the wall ending and the floor (a doorway) he will still get stuck and just sit there.
     
  5. Reevezy

    Reevezy

    Joined:
    May 7, 2013
    Posts:
    26
    That shouldn't be happening, how are you checking if the player is in the air?

    I would use a raycast.
     
  6. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    The only place I check if the character is mid air, is at the bottom of the script in my first post
     
  7. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    Last edited: May 14, 2013
  8. Gale

    Gale

    Joined:
    Jun 22, 2012
    Posts:
    40
    I've had this problem as well. It usually happens whenever you use transform.Translate. What always happened with me was that whenever I pushed against a wall it was like my object would go into the wall then be pushed back out by the physics engine, so it would negate the gravity, even if my object wasn't grounded. How I got over this was by adding small triggers to the left and right of the object that wouldn't allow the object to move in that direction if they got to near a wall. Fixed the problem up quite nice.

    You could also simply use a small raycast I suppose. Whatever works.
     
  9. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    i added things like that to my player in the past but it didnt seem to help with this specific issue :(
     
  10. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    this is so frustrating. One little physics issue is keeping me from releasing this game ><

    It seems to only be getting caught on corners now, and only when jumping up.
     
  11. Pärtel Lang

    Pärtel Lang

    Joined:
    Aug 8, 2012
    Posts:
    9
    Maybe try using a Capsule Collider on your character, it is much smoother than a Box Collider that has sharp edges that could get stuck between other colliders. You could also try using the Character Controller. Btw we have access denied to your google drive link
     
  12. squigglyo

    squigglyo

    Joined:
    May 21, 2012
    Posts:
    107
    ive fixed it! Finally!

    Code (csharp):
    1.  
    2. if(!rigidbody.SweepTest(new Vector3(movement,0,0), out hit, new Vector3(movement,0,0).magnitude))
    3.                 thisTransform.Translate(movement, 0, 0);   
    this little test here has fixed all the problems. I experimented with various checks many times and have finally found one that works.

    Thanks for the help guys.
     
    angelgmoreno likes this.