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?