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?
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.
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.
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$$oved
statement 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
![](https://koobas.hobune.stream/wayback/20220612093743im_/https://answers.unity.com/themes/thub/images/avi.jpg)