2D game movement issues - Stuck On Walls

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.

using UnityEngine;
using System.Collections;

public class NewControls : MonoBehaviour {
	public bool grounded = false;
	public float jumpHeight = 10f;
	public int moveDir = 0;
	public float moveSpeed = 4f;
	
	// Use this for initialization
	void Start () {
	
	}
	public void JumpButton(bool touched)
	{
		if(touched == false)
			return;
		
		if (grounded) {
			rigidbody.velocity = new Vector3 (0f, jumpHeight, 0f);
			grounded = false;
		}
	}
	
	public void MoveButton(bool touched, DIRECTION dir)
	{
		if(!touched)
		{
			moveDir = 0;
			return;
		}
		
		if (dir == DIRECTION.LEFT) {
	
			moveDir = -1;
		
			
		} else if (dir == DIRECTION.RIGHT) {
			moveDir = 1;
		
		}
	}
	
	void Update ()
	{
		if (Input.GetKeyDown ("z")) {
			JumpButton(true);
		}
			
								
		if (Input.GetKey ("left")) {
			MoveButton(true, DIRECTION.LEFT);
		} else if (Input.GetKey ("right")) {
			MoveButton(true, DIRECTION.RIGHT);
		} else if(moveDir != 0){
			MoveButton(false, DIRECTION.LEFT);
		}
			
	}
	
	void FixedUpdate ()
	{
		 float movement;
	
			
			
		
		
		movement = moveDir * moveSpeed;
		movement *= 0.75f;
		movement *= Time.deltaTime;
		//RaycastHit hit;
		//if(!rigidbody.SweepTest(new Vector3(moveDir,0,0), out hit, gameObject.transform.localScale.x / 10))
		transform.Translate(movement, 0, 0);	
		

		// raycast check to see if the player is standing on the ground
		
		if (Physics.Raycast (transform.position, Vector3.down, 0.5f)) {
			if (!grounded  rigidbody.velocity.y < jumpHeight / 10) 
			{				
				grounded = true;
			}
		}else {
			grounded = false;
		}
		
		
	}
}

Add a zero friction physics material to your wall colliders. The character should slide down as expected.

…or if you need your walls to be walkable you can dynamically set your character’s physics materal to 0 friction while ungrounded

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.

That shouldn’t be happening, how are you checking if the player is in the air?

I would use a raycast.

The only place I check if the character is mid air, is at the bottom of the script in my first post

Here is a stripped down version of the game.

https://docs.google.com/file/d/0B5VUJafJTcfJX2RJOVdHMF90M1E/edit?usp=sharing

if you hug the wall on the right and try and jump over it, youl get stuck.

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.

i added things like that to my player in the past but it didnt seem to help with this specific issue :frowning:

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.

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

ive fixed it! Finally!

if(!rigidbody.SweepTest(new Vector3(movement,0,0), out hit, new Vector3(movement,0,0).magnitude))
				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.

1 Like