R
R
raftaliyalu2021-08-25 13:28:22
C++ / C#
raftaliyalu, 2021-08-25 13:28:22

Movement not working, how to fix it?

The player does not move. Doesn't throw errors.

Here is my code:

public float speed;

    private Rigidbody2D rb;
    private Vector2 moveInput;
    private Vector2 moveVelocity;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical") );
        moveVelocity = moveInput.normalized * speed;
    }


    void Fix()
    {
        rb.MovePosition(rb.position + moveVelocity + moveInput);
    }

Probably problems due to Input.GetAxisRaw

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pashara, 2021-08-25
@pashara

The logic of the Fix() method does not work.
You need to call it in FixedUpdate().
Besides, in

rb.MovePosition(rb.position + moveVelocity + moveInput);

written incomprehensibly.
It is necessary to add an offset to the position for the update cycle (fixedDeltaTime, in this case).
moveVelocity already respects the direction of the input (member moveInput yt ljk;ty exbnsdfnmcz d ldb;tybb e;t)
rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question