dsmoothing touch input unity code example
Example 1: how to move player with finger touch in unity
if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) { // get the touch position from the screen touch to world point Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10)); // lerp and set the position of the current object to that of the touch, but smoothly over time. transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime); } }
Example 2: unity mobile touch input
// its a simple jump on tap and while tap controller
// for a geometry dash like game
void MobileController(){
// for the touch movement
if (Input.touchCount > 0)
{
theTouch = Input.GetTouch(0);
if ((theTouch.phase == TouchPhase.Began || theTouch.phase == TouchPhase.Stationary
|| theTouch.phase == TouchPhase.Moved) && isGrounded)
{
myRigidbody.AddForce(Vector3.up * (jumpPower * myRigidbody.mass * myRigidbody.gravityScale * 10.0f));
myAudioPlayer.PlayOneShot(jump);
isGrounded = false;
}
}
}