# 2D Topdown movement without sliding

I’m currently prototyping a game that is 2D topdown but i have trouble with my movement script. I watched the Live Training: Top Down 2D Games video (and allot more video’s) that got me close but i still having problems with the accel and deceleration. I dont have a ship or anything just a person walking so i want him to stop moving as soon as i release my walking keys (WASD). You could say its the same system as Hotline Miami where you only use the mouse for the direction and attacking.

This is my current code that works but doesn’t stop the character right away, it keeps sliding. So my question would be, how to remove the sliding part?

``````using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public float speed = 10;

void FixedUpdate()
{
ControlMouse();
}

void ControlMouse()
{
// capture mouse position. We need to convert between pixels and World Unities
var mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
// Basicly it's looking to the mouse position. And rotating.
Quaternion rot = Quaternion.LookRotation (transform.position - mousePosition, Vector3.forward);

// LOOKING AT MOUSE
// set our gameobject rotation to the calculated one rotation
transform.rotation = rot;
// doesnt changerotation angles for x, y.
transform.eulerAngles = new Vector3 (0, 0, transform.eulerAngles.z);
// prevents from "slide"
GetComponent<Rigidbody2D>().angularVelocity = 0;

// MOVEMENT
// this because moving foward means moving in the Z axis, but this is a topdown game!
// obs.: we still need to press up/down arrows to move!
float input = Input.GetAxis ("Vertical"); //Input.GetAxis ("Mouse X");

GetComponent<Rigidbody2D>().AddForce (gameObject.transform.up * speed  * input );
}
}
``````

I’m actually working on a very similar mechanic for my game, that approximates the Hotline Miami movement (I even used the same video as a start). I believe the part you might have missed from the video was increasing the Linear Drag option on the player’s RigidBody2D component. With a speed of 40, he set the linear drag to 5 to lessen the sliding effect.

For my own game, I used a higher linear drag to almost completely eliminate sliding. Keep in mind that you will have to increase speed in conjunction with drag. Play around with these until you find a combination that works best. Good luck!

Hi, you could set the gravity parameter for the input keys on Edit>Project Settings>Input.

For example, if I increase gravity for horizontal inputs, left and right movements will deaccelerate faster, and you won’t need to change anything to linear drag and speed.

Bye!