2D top-down movement...

I still cannot get around getting my movement to work as I intend.

It works fine with transform.translate, but I still need it to stop at collision, and that is what annoys me the most. So I used rigidbody2D.AddForce instead, and there are two things that I am not happy with, possibly due to my lack of knowledge about RigidBodies.

  • The first: Instead of moving at one speed, it begins slowly then starts to speed up.
  • The second: When I let go of the key, it does not stop…

Here is my code sample:

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

	public float speed;

	void Update() {
		if(Input.GetKey(KeyCode.W)) {
			rigidbody2D.AddForce(Vector2.up * speed);

		if(Input.GetKey(KeyCode.A)) {
			rigidbody2D.AddForce(-Vector2.right * speed);

		if(Input.GetKey(KeyCode.S)) {
			rigidbody2D.AddForce(-Vector2.up * speed);

		if(Input.GetKey(KeyCode.D)) {
			rigidbody2D.AddForce(Vector2.right * speed);

Thank you so much for your assistance, this has been such a great community so far and I am very pleased with the excellent assistance :smiley:

I’m doing a Top-Down too, and one of the best Top-Down movements script I’ve done is this:

public class PlayerMovements : MonoBehaviour{
// Normal Movements Variables
	private float walkSpeed;
	private float curSpeed;
	private float maxSpeed;

	private CharacterStat plStat;

	void Start()
		plStat = GetComponent<CharacterStat>();

		walkSpeed = (float)(plStat.Speed + (plStat.Agility/5));
		sprintSpeed = walkSpeed + (walkSpeed / 2);


	void FixedUpdate()
		curSpeed = walkSpeed;
		maxSpeed = curSpeed;

		// Move senteces
          rigidbody2D.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal")* curSpeed, 0.8f),
			                                   Mathf.Lerp(0, Input.GetAxis("Vertical")* curSpeed, 0.8f));

Hope It will be Helpful! :slight_smile:

Sorry for my Bad English :oops:

#pragma strict

//Inspector Variables
var playerSpeed : float = 10; //speed player moves
var turnSpeed : float = 100; // speed player turns

function Update () 

	MoveForward(); // Player Movement
	TurnRightAndLeft();//Player Turning

function MoveForward()

	if(Input.GetKey("up"))//Press up arrow key to move forward on the Y AXIS
		transform.Translate(0,playerSpeed * Time.deltaTime,0);


function TurnRightAndLeft()

	if(Input.GetKey("right")) //Right arrow key to turn right
		transform.Rotate(-Vector3.forward *turnSpeed* Time.deltaTime);

	if(Input.GetKey("left"))//Left arrow key to turn left
		transform.Rotate(Vector3.forward *turnSpeed* Time.deltaTime);


You wouldn’t need to use rigidbody2D.AddForce to create collision. If you just make your player a rigidbody, and add a collider to the player, use a box collider or capsule collider, and also add a collider to the object you want to collide then when the player runs into the object the player should collide with it.

You can keep it as transform.translate. There shouldn’t be any problems with what i have suggested you to do. If there is, post them up and i will try and help.

Use rigidbody2D.velocity
Like so:
rigidbody2D.velocity =new Vector2(Input.GetAxis(“Horizontal”) * speedValue, Input.GetAxis(“Vertical”) *speedValue);

If thats not enough for your question then i’ll add more details to this.

PS. sorry for typos and unformatted code,dont know how to do this onmobile device.

I think i can help you:

1) The first thing I would do is to collect all input from one frame and apply it as a whole like this :

Vector2 AxisInput;
public float Speed;

void Update()
    Vector2 AxisInput = (new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical")));

    rigidbody.AddForce(AxisInput*Speed, ForceMode.VelocityChange ); // For ForceMode see 2)

2) Your rigidbody is acclerating slowly because of inertia. This can be changed in 2 ways:

  • Choose another ForceMode in the AddForce call (like velocity change demonstrated earlier)

  • (recommended) You can make this like you want via tinkering with mass/drag/angular drag. This will also solve your second problem (movement not stopping after input change)

→ I can’t guarantee these values to fit instantly but my rigidbody worked at this values:

rigidbody mass: ~45

rigidbody drag: 5

rigidbody angular drag: 5 // wont be needed in you case, used for rotation force resistance.

force used in Addforce was 150 (multiplied by axisinput)
tinker with the values and it will work soon!

As always, if you have any questions left, just ask!

Here is a tutorial which shows how to do this: unity 2d top down movement