Having a couple of problems regarding 2D Movement.

Hello there. I’m quite new to coding so these are some nooby questions. I am making a 2D Platformer and I have the movement setup but I have a couple of things that annoy me.

  1. When you are moving and then you take your finger off the key, instead of stopping completely and immediately, it slides for a second or so before stopping. I am not 100% sure how to make it just instantly stop.
  2. The player can stick to walls, meaning it can pretty much climb walls. I want it to just fall when it hits a wall, but I am not 100% sure how to do that.

Here is my code:

public float jumpHeight;
public float moveSpeed;

public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Use this for initialization
void Start () {

}

void FixedUpdate(){
	grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}

// Update is called once per frame
void Update () {

	if (grounded) {
		doubleJumped = false;	
	}
					

	if (Input.GetKeyDown (KeyCode.W)&& grounded ) {
		GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);	
		Jump ();
	}

	if (Input.GetKeyDown (KeyCode.W) && !doubleJumped && !grounded ) {
		GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
		Jump ();
		doubleJumped = true;
	}

	if (Input.GetKey (KeyCode.D)) {
		GetComponent<Rigidbody2D>().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);	

	} if (Input.GetKey (KeyCode.A)) {
		GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y); }

}
public void Jump() {
GetComponent ().velocity = new Vector2 (GetComponent ().velocity.x, jumpHeight);
}
}

I’m making a similar game at the moment, and have just come across both of these problems.

Your first problem is relatively easy to solve. You are already setting the velocity of the GameObject’s Rigidbody if the player is pressing the A or D keys, so all you need to do to stop instantly is to restructure your existing movement code a bit, to look like this:

if (Input.GetKey (KeyCode.D)) 
{
         // Your code to move left
} 
else if (Input.GetKey (KeyCode.A)) 
{
        // Your code to move right
}
else
{
    GetComponent<Rigidbody2D>().velocity = new Vector2 (0, GetComponent<Rigidbody2>().velocity.y);
}

This means that if you are pressing A, you move left, or if you are pressing D you move right, but if you press neither, your velocity in the X direction is set to nothing.

It’s worth pointing out that you are calling GetComponent() a lot in your code. You probably wont notice any performance loss by doing this a few times, but if you are constantly searching for components you might find your game slowing down. A better way to do this is to create a variable to hold your rigid body, and only search for it once.

private RigidBody2D body;

void Start()
{
  body = GetComponent<Ridigbody2D>();
}

void Update()
{
  ...
   body.velocity = new Vector2(...)
   ...
}

For your second question, I’m still working on that!
I’ve found that the 2D asset package has a similar character controller to what I’m looking for, you can import it into your own project to have a look. They stop the player sticking to walls by adding an extra BoxCollider2D to edges of platforms, and give it a physics material with 0 friction.