- Home /
How to call the same functions both in FixedUpdate and LateUpdate?
I know this might sound weird cause i'm a beginner, but how do i call the same function in different places? I can't call Move() in the FixedUpdate because the Touch input gets delayed, but also i have to, cause i have a AddForce, and physics things has to be called in FixedUpdate right? How to solve this?
[alt text] [1]: /storage/temp/162725-code.png
Answer by Eno-Khaon · Jul 04, 2020 at 06:08 AM
Update() and, by extension, LateUpdate(), cycle with every rendered frame of gameplay. That is also where inputs are processed.
FixedUpdate() is where physics are processed. In order for them to be consistent, they must be processed at regular intervals, regardless of your framerate. As such, they are not guaranteed to coincide with your inputs. This is also the ideal place to handle physics functionality.
That said, while you can access inputs during FixedUpdate(), you can't guarantee that their state will consistently be what you want it to be. For example, if physics cycles 50 times per second (default) and your framerate is 200 frames per second, you essentially have a 1-in-4 chance to successfully trigger OnDown and OnUp-type inputs during FixedUpdate(), since the state will continue to change each frame, regardless of the physics cycles.
So, how do you work under these conditions?
First, let's take a look at Update() vs. LateUpdate(). As a basic principle, you can safely assume that LateUpdate() should be reserved primarily for camera movement, so that any non-physics-driven actions have already been prepared/performed for the current frame.
Therefore, Update() is typically the better place to handle player input.
Okay, NOW let's look at how you combine input with physics.
You'll want to look at preparing your inputs during Update() to be processed as physics interactions in FixedUpdate(). You've already done most of the work, so here's how it looks with a little bit of reorganization:
// This will be used to send single inputs at a time
bool newTouch = false;
// This will be used to store input to send along when needed
Vector2 playerInput;
void Update()
{
newTouch = GetInput();
}
private bool GetInput()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Began)
{
// This converts the horizontal touch position to a 0-1 range
float touchSide = touch.position.x / Screen.width;
// By converting this to a -1 or 1 value, it removes the need
// for separate, similar functions. This may or may not be what
// you have in mind, but is here to demonstrate other options
touchSide = Mathf.Sign(touchSide * 2 - 1);
if(touchSide < 0f)
{
StartCoroutine(RotateLeft());
}
else// if(touchSide >= 0f)
{
StartCoroutine(RotateRight());
}
// The input is finally assigned to a variable here, for use in FixedUpdate()
playerInput = new Vector2(sideForce * touchSide, upForce);
// There was new touch input this frame
return true;
}
}
// There was not or was not usable touch input this frame
return false;
}
void FixedUpdate()
{
if(newTouch)
{
Move();
}
}
private void Move()
{
// Simplifying your example, since zeroing out velocity then
// adding force is the same as setting velocity to that amount
// ... Also, applying a 1-frame input with gradual force application
// is essentially the same as dividing the force applied by 50 (default)
rb.velocity = playerInput;
// Now that the new touch has been used from FixedUpdate(),
// Reset the boolean for reuse next time
newTouch = false;
}
Now, I've made a large number of tweaks all over the place (while also trying to keep everything intact). Granted, some of it certainly comes from personal tastes, so experiment with it an find a layout that suits you best.
I hope this may help get you started on how you can look at separating your input from the result of that input where it's appropriate.
THANKS DUDE!!! where did you take this from? amazing content, very well explained.
Dude, just to let you know, this indeed solve my problem, but gave me a whole bigger bug, when i "split" the movement it give a an inconsistence behavior, sometimes the player's rigidbody2D was not responding the touch input, just like in the video below, so i let everything in update, it seems in gameDev you simply can't have anything you want in your game hahaha. thanks again.
Your answer
Follow this Question
Related Questions
Function call is not working 0 Answers
Integer Getter Method always returns zero 3 Answers
How add Compontent B to all Childs with Component A (e.g. Script) 1 Answer
Sprites that don't go out of the screen? 1 Answer
Bouncing problem 0 Answers