# Why is my player movement code not working?

I'm trying to make my 2D character able to float around the screen, but my code isn't working:

using UnityEngine;

public class playermovement : MonoBehaviour
{

public float moveSpeed = 5f;

public Rigidbody2D rb;

Vector2 Movement;

public Vector2 Movementspeed { get; private set; }

// Update is called once per frame
void Update()
{
Movement.x = Input.GetAxisRaw("Horizontal");
Movement.y = Input.GetAxisRaw("Vertical");
}

void FixedUpdate()
{
rb.MovePosition(rb.position + (Movement * Movementspeed * Time.fixedDeltaTime));
}
}

• Is MovementSpeed ever assigned a value? The default for new Vector2() would have X = 0, Y = 0 so Movement * (0) * Time.fixedDeltaTime = 0. Oct 1, 2019 at 20:19

Let's go over what your code is doing, and perhaps we can point some things out.

You use the variable Movement to capture the input for each axis at each update frame:

void Update()
{
Movement.x = Input.GetAxisRaw("Horizontal");
Movement.y = Input.GetAxisRaw("Vertical");
}


Capturing this in a Vector2 makes sense since you're moving in 2d, and doing it in the Update callback makes sense since you want to read the input every frame. So far so good.

Then in fixed update, you're moving the position of the Rigidbody2d by doing so:

rb.MovePosition(rb.position + (Movement * Movementspeed * Time.fixedDeltaTime));


What is the value of the variable Movementspeed at this point? Unless you have used the property setter to give it a value from somewhere else, it would default to (0,0). So the value of the statement (Movement * Movementspeed * Time.fixedDeltaTime) will return 0. So essentially you're setting the position of the rigidbody to its current position.

But really think about what you're trying to do in that statement. Movement is a 2d vector that represents your direction of movement, Time.fixedDeltaTime ensures that we move by a consistent amount no matter the variance in time between fixed update calls, but what is the purpose of Movementspeed? You're doing a cross product here when I don't think you intend to. You don't really need to use another Vector2 to represent your speed, just use a float. Remember that any vector multiplied by a float just changes the magnitude of that vector. The direction stays the same.

So what you probably want to do here is:

rb.MovePosition(rb.position + (Movement * moveSpeed* Time.fixedDeltaTime));


where moveSpeed is the float variable you've defined at the top of the script.

• Maybe OP actually wants to define the movement speed as a Vector2 because the y want their horizontal and vertical movement speeds to be different? Oct 3, 2019 at 11:02
• @Philipp Yeah thats a totally fair call out, and I should point out I made an assumption there simply because I noticed the unused moveSpeed float variable. But yes, if OP indeed wants to add a speed modifier for each axis, the Vector2 multiplication is totally ok. Oct 3, 2019 at 15:30

I fixed it thanks for the help? it works now! this is how it ended up:

using UnityEngine;

public class playermovement : MonoBehaviour {

public float moveSpeed = 5f;

public Rigidbody2D rb;

Vector2 movement;

// Update is called once per frame
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
}

void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}


}

• Its looks like you have used the way @Shervin suggested. Any way if any answer helps you out make sure to up vote him and if An answer resolve or show you the point then you have to Accept it. So the other user in need should see the correct answer. Oct 2, 2019 at 8:00
• how do I up vote someone Oct 2, 2019 at 12:18
• Each answer has up and down arrow in front of it, To up vote some one click up arrow and to down vote click down arrow. and to accept Answer click Tick Mark near Down arrow. Oct 2, 2019 at 12:58
• @Swati New users need to collect 15 reputation points before they can upvote answers, but Jonah Tieman should be able to accept the answer which solved their problem by clicking on the checkmark-icon next to the answer. Oct 3, 2019 at 11:00
• Yes. You are right, I was just trying to let @JonahTieman know about up vote and down vote and accepting the answer. Oct 3, 2019 at 11:04