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 an object will follow the position on where you touch, so when you move your finger over the screen the object follows. But looks like I have hit a crossroad with two problems!
First, 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 move between position when I press quickly?
Second, there is the problem when you hold down more than one fingers on the screen the ball will make "swaps" between positions. How do I set it to just work with one finger?
Here is the code:
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public GameObject character;
public float speed = 50.0f;
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);
}
if(Input.touchCount > 1)
{
//What should I write here? Send back null?
}
}
Here is the code for checking input:
using UnityEngine;
using System.Collections;
public class TouchScript : MonoBehaviour
{
void OnGUI()
{
foreach(Touch touch in Input.touches)
{
string message = "";
message += "ID: " + touch.fingerId + "\n";
message += "Phase: " + touch.phase.ToString() + "\n";
message += "TapCount: " + touch.tapCount + "\n";
message += "Pos X: " + touch.position.x + "\n";
message += "Pos Y: " + touch.position.y + "\n";
int num = touch.fingerId;
GUI.Label(new Rect(0 + 130 * num, 0, 120, 100), message);
}
}
}
Thank you for your help!
↧