Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by rodrigopereiramelo · Jul 04, 2020 at 04:23 AM · scripting problemphysics2dfunctionmethod

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

code.png (19.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rodrigopereiramelo · Jul 04, 2020 at 03:32 PM 0
Share

THANKS DUDE!!! where did you take this from? amazing content, very well explained.

avatar image rodrigopereiramelo · Jul 22, 2020 at 05:37 PM 0
Share

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.

https://www.youtube.com/watch?v=RjJ$$anonymous$$dj_YkbA

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges