# 2D Movement Help!!

**URL:** https://discussions.unity.com/t/2d-movement-help/655497
**Category:** 2D
**Tags:** 2D
**Created:** [February 15, 2017, 3:57pm UTC](https://discussions.unity.com/t/2d-movement-help/655497 "2017-02-15T15:57:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![JayBeeArr](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JayBeeArr](https://discussions.unity.com/u/JayBeeArr)
#### Post date: [February 15, 2017, 3:57pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/1 "2017-02-15T15:57:34Z")

</div>

Hello, I need to move my player not only up and down but also left and right. But this script is only allowing me to move up and down and not left and right as well. Below is my script:

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour {

    public Rigidbody2D rb2D;
    public float speed = 10f;

    // Use this for initialization
    void Start ()
    {
        rb2D = GetComponent<Rigidbody2D> ();
    }
   
    // Update is called once per frame
    void FixedUpdate ()
    {
        float Horizontal = Input.GetAxis ("Horizontal") * Time.fixedDeltaTime * speed;
        float Vertical = Input.GetAxis ("Vertical") * Time.fixedDeltaTime * speed;

        Vector2 newPositionX = rb2D.position + Vector2.right * Horizontal;
        Vector2 newPositionY = rb2D.position + Vector2.up * Vertical;

        rb2D.MovePosition (newPositionX);
        rb2D.MovePosition (newPositionY);
    }
}

```

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [February 15, 2017, 4:25pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/2 "2017-02-15T16:25:28Z")

</div>

I’d hazard a guess that MovePosition() is probably overwriting newPositionX with newPositionY each time. You need to rewrite it so MovePosition only runs once with a vector2 holding both new positions. FixedUpdate() should only be for effecting rigidbody physics so you should be running this in update() anyway. And always use Time.DeltaTime.

---

<div class="post-metadata">

### Author: ![LiterallyJeff](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/literallyjeff/32/900755_2.png) [@LiterallyJeff](https://discussions.unity.com/u/LiterallyJeff)
#### Post date: [February 15, 2017, 4:26pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/3 "2017-02-15T16:26:52Z")

</div>

```csharp
rb2D.MovePosition (newPositionX);
rb2D.MovePosition (newPositionY);

```

Your player only moves up and down because these two lines conflict. MovePosition moves your character’s rigidbody to an explicit location. Then you call it a second time, overwriting the first. If you changed the order of the lines, you would only be able to move left/right.

The way to fix this is to call MovePosition just once, with both the X and Y position combined.

```csharp
private Vector3 movementInput;

private void Update() {
    movementInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
private void FixedUpdate() {
    Vector3 offset = movementInput * Time.deltaTime * tweenSpeed;
    Vector3 newPosition = rb2D.position + offset;

    rb2D.MovePosition(newPosition);
}

```

> [@jackhearts](#):
>
> FixedUpdate() should only be for effecting rigidbody physics so you should be running this in update() anyway. And always use Time.DeltaTime.

Getting the inputs in Update is the way to go, but the rest doesn’t really make a difference. The position of the rigidbody will only be updated in FixedUpdate when using MovePosition, so there’s no reason to run this logic every frame. Since it’s running on the fixed timestep, using “fixedDeltaTime” is correct to make the player’s speed independent of time step, however “deltaTime” will also return “fixedDeltaTime” when used within a FixedUpdate function so either will work.

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [February 15, 2017, 5:00pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/4 "2017-02-15T17:00:08Z")

</div>

true, only glanced and should’ve specified splitting inputs and physics. 👀

Using Time.DeltaTime is better practice because the correct one will always be returned. But yea, fixeddelta within fixed works too.

---

<div class="post-metadata">

### Author: ![JayBeeArr](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@JayBeeArr](https://discussions.unity.com/u/JayBeeArr)
#### Post date: [February 15, 2017, 5:31pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/5 "2017-02-15T17:31:35Z")

</div>

Thanks Jeffreyschoch and JackHearts! I changed the code so that it uses a Vector 2 rather than a Vector 3 because I’m working in a 2D project but they work perfectly! (I dunno if that is a big change to how things work (using Vector 2 instead of Vector 3) but it still works regardless).

I think also referencing Vectors is something new I learned as well so I will be implementing that in future projects!

---

<div class="post-metadata">

### Author: ![LiterallyJeff](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/literallyjeff/32/900755_2.png) [@LiterallyJeff](https://discussions.unity.com/u/LiterallyJeff)
#### Post date: [February 15, 2017, 6:02pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/6 "2017-02-15T18:02:40Z")

</div>

Position is a Vector3, so be aware that if you set position to a Vector2, the Z value will be assigned 0 implicitly.

For example:

```csharp
Vector3 position = new Vector3(10,10,3);
// position is now (10, 10, 3)
position = new Vector2(5, 5);
// position is now (5, 5, 0)

// reversed:
Vector2 position = new Vector2(5, 5);
// position is now (5,5)
position = new Vector3(10,10, 3);
//position is now (10, 10)

```

---

<div class="post-metadata">

### Author: ![jackhearts](https://avatars.discourse-cdn.com/v4/letter/j/e274bd/32.png) [@jackhearts](https://discussions.unity.com/u/jackhearts)
#### Post date: [February 15, 2017, 8:56pm UTC](https://discussions.unity.com/t/2d-movement-help/655497/7 "2017-02-15T20:56:29Z")

</div>

JayBee. vectors are convenient holders for floats so quite essential. A vector3 being x,y,z and vector2 x and y. The 2D scripting elements are simply ignoring the z axis. MovePosition for instance will only affect x and y so the z of the vector3 won’t make a difference.You can access each value easily with myVector.x, etc. So your original code can be written like this…

```csharp
float Horizontal = Input.GetAxis ("Horizontal") * Time.fixedDeltaTime * speed;
    float Vertical = Input.GetAxis ("Vertical") * Time.fixedDeltaTime * speed;

    Horizontal = rb2D.position.x + 1 * Horizontal;
    Vertical = rb2D.position.y + 1 * Vertical;

    Vector2 newPos = new Vector2(Horizontal, Vertical);

    rb2D.MovePosition (newPos);

```

When working in 2D you should be able to use vector2 pretty much all the time without ill effect. Just be aware of what jeffrey mentioned. If you change a position directly like transform.position = new vector2() then the z will set to zero. That may cause a problem if you’ve got sprites at certain z values for whatever reason.
