I have been 3 days trying to solve this problem but still nothing. I am working on a game where by using the touch screen of the phone, a ball would move to the X position of your finger when you touch the screen, and do something different when while holding your first finger, you tap with the second finger.
The phone does detect whether it is the first touch or the second touch, and the second does what it is supposed to do. But the first touch does not do what it is supposed to. I know it is detected, because I already tested it. But there must be some mistake with the way I am making the ball move.
This is my code.
I have tried using AddForce, but I don't want the ball to keep going after I release my finger. Also tried many types of touches, also tried using a Joystick, but after I release my finger, the ball keeps going (a little bit further).
foreach (Touch touch in Input.touches)
{
if (touch.fingerId == 0)
{
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0f;
if (touchPosition.x > transform.position.x)
{
transform.position = new Vector3(transform.position.x + 10, transform.position.y, transform.position.z);
}
else if (touchPosition.x < transform.position.x)
{
transform.position = new Vector3(transform.position.x - 10, transform.position.y, transform.position.z);
}
}
if (touch.fingerId == 1)
{
Accelerate();
}
}
I would like the code to detect whether my finger is more to the left or more to the right of the ball, and move the player to the left or right accordingly. I am also trying to make it move "smoothly", not too fast, not too slow. And afterwards, if I move my finger without lifting the finger up, the ball would still follow it.
I have spent 3 days trying different ideas....I appreciate any help. Also already searched in many posts, and unity's documentation and forum. Also searched for youtube videos...but have not found nothing really useful.
edit. forgot to mention. This is exclusively for android at the moment.