- Home /
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 );
}
}
@bspin Have you tried just setting the Velocity to 0 when you release the button? As in Vector2 zeroVel = new Vector2(0,0); Rigidbody2D.Velocity = zeroVel;
Answer by thecox801 · Mar 31, 2015 at 03:31 PM
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!
Thanks ill try some different value's.
For now i've just removed the Rigidbody and use the normal position changes.
Answer by FooTaylor · Mar 07, 2016 at 11:12 AM
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!
Your answer
Follow this Question
Related Questions
Character movement problem, floats away 1 Answer
2D Directional top down movement,Topdown 2d Directional Movement 0 Answers
How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer
(2D Movement) How do I make my sprite move up, down, left, and right, without moving diagonal? 1 Answer
Top-down movement in 2D 1 Answer