Mobile Input Help

Updated on June 25, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on June 25, 2018

I am using this script for movement, left and right, however it feels like it is stuttering a bit any advice?

 

Also want to make it smoother if possible

 

 

 

 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class BlueBlock : MonoBehaviour
{
 
private Vector2 startTouch, swipeDelta;
 float speed = 5.0f;
 
 // Update is called once per frame
 void Update()
 {
 
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
 {
 startTouch = Input.touches[0].position;
 }
 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
 {
 swipeDelta = Vector2.zero;
 swipeDelta = Input.touches[0].position - startTouch;
 
if (swipeDelta.magnitude > 125)
 {
 float x = swipeDelta.x;
 
//left or right
 
if (x < 0)
 {
 transform.Translate(Vector2.left * Time.deltaTime * speed);
 }
 
else
 {
 transform.Translate(Vector2.right * Time.deltaTime * speed);
 }
 }
 }
 if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
 {
 startTouch = Input.touches[0].position;
 swipeDelta = Vector2.zero;
 swipeDelta = Input.touches[0].position - startTouch;
 
}
 }
 }
 
  • Liked by
Reply