The relationship between these four things is so odd that I don’t know how to fix it now that a problem has occurred.
This is a very simple framework for role control scripts
void Move()
{
float moveMultiple = Input.GetAxis("Horizontal");
if (moveMultiple != 0)
{
rig.velocity = new Vector2(moveMultiple * moveSpeed * Time.deltaTime, rig.velocity.y);
}
}
void Jump()
{
if (Input.GetKeyDown(KeyCode.W))
{
rig.velocity = new Vector2(rig.velocity.x, jumpForce * Time.deltaTime);
}
}
void FixedUpdate()
{
Move();
Jump();
}
The first problem arises here [key press event] is not listened in FixedUpdate. When I press the jump key, the character sometimes jump up and sometimes does not jump up.
To solve this problem I made some adjustments to my script
void FixedUpdate()
{
Move();
}
void Update()
{
Jump();
}
I put jump() into update, however my character can’t jump normally. When I press jump, the character only moves a few pixels.
The unity documentation says that object movement should be manipulated in fixedupdate. This pattern is very confusing to me, I have to listen to the button in update and control the jump in fixedupdate. Adding a variable to specifically pass the keystroke state would make the script very complicated, and since the two updates are not synchronized it could lead to lost keystrokes in between.
I would like to know if there is a way to solve this problem so that I can listen to the keystrokes in fixedupdate or set the speed in update,The relationship between these four things is so odd that I don’t know how to fix it now that a problem has occurred.