Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by LK84 · Sep 30, 2016 at 03:46 PM · rigidbodyfixedupdateswipeupdate functiontouchphase

Swipe Gesture combined with Physics -- Update or FixedUpdate ?

Hello,

I am working on a simple tennis game. According to the velocity of the swipe gesture a Torque is added to the tennis racket using Rigidbody.AddTorque(racket, ForceMode.Acceleration) at every frame when the finger is moving over the screen. I am using switch-case to distinguish between the different touch gestures;

    Vector3 Torque;
     float timeStart;
     Vector3 touchStart;
     float   touchSpeed;
     Rigidbody tennisRacket;
  Touch thisTouch;
     
     void Update(){
     
       if (Input.touchCount > 0) 
                 {   thisTouch = Input.GetTouch(0);
                     switch (thisTouch.phase)
                     {
                         case TouchPhase.Began:
                             touchStart = thisTouch.position;
                             timeStart = Time.time;
                             break;
                         case TouchPhase.Moved:
                          
                            
                                 if (Mathf.Abs(thisTouch.position.y - touchStart.y) > comfortZone)
                                 {
                                     if ((Time.time - timeStart) <= 1.0f)
                                     {
                                         touchSpeed = Mathf.Clamp(thisTouch.deltaPosition.y / thisTouch.deltaTime * 10.0f / Screen.height, 20.0f, 95.0f);
         
                                         Torque = Vector3.right * touchSpeed;
                                         tennisRacket.AddTorque(Torque,ForceMode.Acceleration);
         
                                     }
         
                                 }
                            
                         
                             break;
                          case TouchPhase.Stationary:
                                 StopRacket();//Function which stops racket instantly
                                  break;
         
                           case TouchPhase.Ended:
                                 StopRacket();//Function which stops racket instantly
                                  break;
                         default:
                             break;
                     }
                 }
             }
    

My question now: I know that applying force/torque to Rigidbodies should happen in the FixedUpdate() Method. On the other Hand, I'm not sure if some Touch gestures won'T be recognied in Fixed Update(). What is the most elegant solution to my problem?

Comment
Add comment · Show 1
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 LK84 · Oct 02, 2016 at 04:07 PM 0
Share

anyone has an idea?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Oct 02, 2016 at 05:49 PM

You should always listen for user input in Update(). And you should always apply Physics calculations in FixedUpdate(). So, you keep the vast majority of your code as it is, but move the line:

      tennisRacket.AddTorque(Torque,ForceMode.Acceleration);

into FixedUpdate.

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 Owen-Reynolds · Oct 02, 2016 at 06:05 PM 0
Share

The important thing here is input checking has to be in Update. Unity only handles it before each Update. If you read input during FixedUpdate, you might use the same input twice, or miss one.

For applying physics changes, I don't know of a similar, universal "if won't work" reason not to do it in Update. I think putting it in Fixed is just often a safe, simple choice.

avatar image LK84 · Oct 02, 2016 at 09:10 PM 0
Share

I know that Physics have to be in FixedUpdate and User Input in Update, but since the physics strongly depend on the user input (they actually have to be updated in realtime) things are different here. $$anonymous$$aybe you misunderstood my question, I am not a beginner who doesn't know those basics, I'm more curious about the consequences when Update and FixedUpdate interact so strongly. To give you an example what I mean: When I implement the changes as you suggested and set Torque=0 every time the TouchPhase.$$anonymous$$ovedstatement is false. As @Owen-Reynolds mentioned, the Torque might be added twice for the same input (actually I experienced that during debugging). So my conclusion is none of the to options seems to be flawless, I am just looking for the option causing less damage

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

69 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

Related Questions

how to drag a 2d object in one direction but even finger is not over the object,either its up or down than opbject but it should be drag in the direction of moveing finger 0 Answers

Unable to use Swipe and Unity UI together 0 Answers

GetAxis being missed in FixedUpdate work around? 1 Answer

Stupid question but how do you set aside a variable? 0 Answers

Move a RigidBody with MovePosition up and down 1 Answer


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