0
\$\begingroup\$

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));
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Is MovementSpeed ever assigned a value? The default for new Vector2() would have X = 0, Y = 0 so Movement * (0) * Time.fixedDeltaTime = 0. \$\endgroup\$
    – Romen
    Commented Oct 1, 2019 at 20:19

2 Answers 2

1
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Philipp
    Commented Oct 3, 2019 at 11:02
  • \$\begingroup\$ @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. \$\endgroup\$
    – Shervin
    Commented Oct 3, 2019 at 15:30
0
\$\begingroup\$

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

}

\$\endgroup\$
5
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Swati
    Commented Oct 2, 2019 at 8:00
  • \$\begingroup\$ how do I up vote someone \$\endgroup\$ Commented Oct 2, 2019 at 12:18
  • \$\begingroup\$ 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. \$\endgroup\$
    – Swati
    Commented Oct 2, 2019 at 12:58
  • \$\begingroup\$ @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. \$\endgroup\$
    – Philipp
    Commented Oct 3, 2019 at 11:00
  • 1
    \$\begingroup\$ Yes. You are right, I was just trying to let @JonahTieman know about up vote and down vote and accepting the answer. \$\endgroup\$
    – Swati
    Commented Oct 3, 2019 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .