I have kind of a "bug" in my code that I just can't find why its happening and how to fix it (I'm a beginner in unity and even more in mobile games in unity )
I have player movement from side to side using the touch but the problem is I want the player to move smoothly when sliding the finger from side to side but the code I have also moves the player where you tap and not only when you slide.
Any help would be appreciated, thank you all :)
My code:
public float playerspeed = 500f;
public float directionalspeed;
void Start()
{
}
void Update()
{
float movehorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + movehorizontal, -1f, 1f), gameObject.transform.position.y, gameObject.transform.position.z), directionalspeed * Time.deltaTime);
// -------------------------MOBILE CONTROLS SECTION STARTS HERE ------------------------------------------------
GetComponent<Rigidbody>().velocity = Vector3.forward * playerspeed * Time.deltaTime;
//collects the postion of the finger on the screen
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
//if there are more then 0 fingers on the screen , move the ball smoothly on the X axis to where the finger is pointing
if (Input.touchCount > 0)
{
transform.position = new Vector3(touch.x, transform.position.y, transform.position.z);
}
}