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 );
}
}