Y
Y
Yura Mailler2021-08-04 16:06:03
Unity
Yura Mailler, 2021-08-04 16:06:03

Due to Time.deltatime, the player's movement distance is not controlled, what should I do?

Hello! I'm making a game and I need the character to move smoothly on the grid, I made the following code

public float Speed;

    private Rigidbody2D _rigibody2D;


    void Start()
    {
        _rigibody2D = GetComponent<Rigidbody2D>();
    }

    
    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.S)) {  
            transform.Translate(new Vector2(0.0f , -Speed * Time.deltaTime));
        }
    }

After pressing the S button, the character moves to the distance specified in Speed, well, everything works, but only he does it very abruptly, so I added Time.deltaTime, after which the character began to move smoothly, but the distance
changes, for example: 0.245 or 0.753 ( this is after subtracting the number from Speed, (the numbers are invented 0.245 and 0.753)) FixedUpdate works at a certain point (not like Update) after moving, to move again you have to wait about 1 second before moving again, I would like to not have this delay, but because of Update, the old problem reappears. What can be done to solve my problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-08-04
@freeExec

Do not use Inputin FixedUpdateread the documentation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question