- Home /
Mobile game Player follow finger input problem (C#)
Hello my fellow Unities. I'm developing a 2D mobile game using touch. I'm trying to make it so that you can only hold down one finger on the screen when you do the player will follow the position on where you touch, so when you move your finger over the screen the object follows. But I have a problem
If I touch the screen on different location quickly the object will swap over to the position where i pressed, how can I make the ball just follow the finger when I hold down on the screen and not jump between position when I press one point of the screen and then lift the finger of the screen to press another position of the mobile screen?? Here's the code for the player following the finger:
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public GameObject character;
public float speed = 50;
void Update ()
{
if(Input.touchCount == 1)
{
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y, 10.0f));
character.transform.Translate(Vector3.MoveTowards(character.transform.position, target, speed * Time.deltaTime) - character.transform.position);
}
}
}
Answer by ShadoX · Apr 05, 2016 at 12:15 PM
You could add a small timer and check that the player has been holding the the finger on the screen for a set amount of time before starting to move the ball.
Answer by saschandroid · Apr 05, 2016 at 12:29 PM
You should look into the different TouchPhases and use them for your input. For example to move the object only if you are moving your finger you can use:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved )
{
// Update position
}
That almost worked, only problem is that if I first hold down on the screen and move the finger the player follows the finger and if I release and touch the screen on another position only once the player does not move.
BUT if I release the finger and then drag the finger on another position on the screen the player follows (because of the TouchPhase.$$anonymous$$oved). How can I do it so the finger needs to touch the player again to make it move?
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Input.GetTouch(0).position.x and TouchPhase.Began 1 Answer
Swipe and Hold to move character . 1 Answer
How to make responsive Touch inputs? 1 Answer
Distribute terrain in zones 3 Answers