Question by
busraozcan · Apr 05, 2021 at 01:38 PM ·
movement scriptplayer movementtouch controlsmobile devicestouch screen
how to move by touching a point on mobile device like join clash game
I want to make a player movement like join clash game. Sliding left and right working good for now but going straight doesn't work well. But i want to go straight when i keep holding a one point. How can i do it? This is my code :
**private void Update()
{
Mathf.Clamp(transform.position.x, -8, 8);
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
tempDirection = Vector3.zero;
direction = Vector2.zero;
tempTime = 4;
break;
case TouchPhase.Moved:
tempTime += speed * 0.1f;
Debug.Log(touch.radius);
direction = (touch.position - startPos).normalized;
if (tempTime < 10 && tempTime >= 4)
{
rb.velocity = new Vector3(rb.velocity.z, rb.velocity.y, 5);
direction *= tempTime;
rb.velocity = new Vector3(direction.x, rb.velocity.y, rb.velocity.z);
//Debug.Log(tempTime);
}
else
{
direction *= 11;
rb.velocity = new Vector3(direction.x, 0, rb.velocity.z);
}
break;
case TouchPhase.Stationary:
//rb.velocity = new Vector3(0, 0, 5);
break;
case TouchPhase.Ended:
rb.Sleep();
break;
}
}
}**
Comment