should i be using update or fixed update for movement and jumping?
I'm so sorry for the text wall
I'm in the very very early stages of learning how to use unity. I first followed a tutorial that said i should use fixedupdate when using force. i did this. i made simple wasd movement, but the tutorial briefly mentioned that it was not ideal to have the getkey components in fixed update and didnt say much else.
Then i tried to make jumping with GetKeyDown and not all key presses were registering. simple googling told me that i needed to change to update.
Well now I'm confused. I need to use update because I need accurate inputs, and i need to use fixedupdate because those inputs trigger forces. it does seem to work fine in update, but i assume im not doing things "correctly".
i tried creating definitions and stuff to separate the getkey from the fixed update like the tutorial briefly mentioned but obviously idk what im doing because it just mimics the original problem i had where the jump was not always working:
private bool forward;
private bool backward;
private bool left;
private bool right;
private bool jump;
void Update()
{
forward = Input.GetKey(KeyCode.W);
backward = Input.GetKey(KeyCode.S);
left = Input.GetKey(KeyCode.A);
right = Input.GetKey(KeyCode.D);
jump = Input.GetKeyDown(KeyCode.Space);
}
void FixedUpdate()
{
if (forward)
{
Playerbody.AddForce(0, 0, moveforce*Time.deltaTime);
}
if (backward)
{
Playerbody.AddForce(0, 0, -moveforce * Time.deltaTime);
}
if (left)
{
Playerbody.AddForce(-moveforce * Time.deltaTime, 0, 0);
}
if (right)
{
Playerbody.AddForce(Vector3.right*moveforce*Time.deltaTime);
}
if (jump)
{
Playerbody.AddForce(0,10,0,ForceMode.Impulse);
Debug.Log("space");
}
}
like i said, i know this is wrong and all i did was make the code (and my explanation) unnecessarily long, but how do i use FixedUpdate with force without missing inputs? and should i use fixedupdate instead of update for something like this? am i overthinking it?
Your answer
Follow this Question
Related Questions
What is the difference between Update() , LateUpdate() , FixedUpdate(), and when i should use them 3 Answers
Only Disable One Update on a Script 1 Answer
Speed inconsistent even when using time.dT + Fixed Update 3 Answers
How do I call a function when my slider is at max value? 0 Answers
FixedUpdate Crash when try to save prefab in runtime. 0 Answers