There was a discussion on the Unity channel over on Discord which I think would be beneficial to be put on the forum.
Environment : Unity
Topic : Touch input query
Whats in scene : simple box at origin with below script attached.
Orthographic camera at Z -10 from origin.
the question I believe was how to track the first touch that was made on mobile and ignore all others to then move a player. until all other fingers are lifted and the initial press once more sets the destination again.
I have had a play around this morning and got a little test sandbox, have a look rip it apart and see if any of the touch info helps, ive tried to comment as best I could in a rush.
the other stuff was just me playing around to get something working quickly as possible as I dont have the rest of your code. So have a look at the touch stuff and see if that helps any.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchToMove : MonoBehaviour {
public float playerMoveSpeed = 2f; // used for the move towards later on
private int m_FingerIdTracker; // this will be to track id of finger that is down
void Update () {
// do we have more than one touch on screen?
if (Input.touchCount > 0)
DoTouchControlStuff();
}
void DoTouchControlStuff()
{
// get the touch info for the first touch that hit the screen and store it
Touch myTouch = Input.GetTouch(0);
// now lets get the unique finger ID for that touch,
// only if it has just began, AND theres only one touch hitting the screen
if(myTouch.phase == TouchPhase.Began && Input.touchCount == 1)
{
m_FingerIdTracker = myTouch.fingerId;
Debug.Log("new id set as new single touch detected");
}
// since we know that our fingerid was only for that first press, lets compare it to the frst one thats
// currently on the screen, just in case you lift 1st finger and leave 2nd on.
if(myTouch.fingerId == m_FingerIdTracker)
{
// if its the same, great, now move our player around
MoveTowardsInScreenSpace(myTouch.position);
}
}
void MoveTowardsInScreenSpace(Vector2 cursorPos)
{
// move player based on the cursorPos which is the 2d position in screen space of the touch were using
// just using a simple 2d view here to illustrate with the player
// create ray
Ray ray = Camera.main.ScreenPointToRay(cursorPos);
// create the aim plane
Plane groundPlane = new Plane (
// just need to declare 3 corner points to create the plane, as it iterpolates the 4th corner.
// doing it this way, creates the plane dead centre of the ball
new Vector3(transform.position.x + 10f, transform.position.y-10f, transform.position.z),
new Vector3(transform.position.x - 10f, transform.position.y-10f, transform.position.z),
new Vector3(transform.position.x + 10f, transform.position.y+10f, transform.position.z));
// used for raycasting out, logs hit distance
float rayDistance;
if(groundPlane.Raycast(ray, out rayDistance))
{
Vector3 destinationPosition = ray.GetPoint(rayDistance);
// lock the Y and Z positions
destinationPosition.z = transform.position.z;
destinationPosition.y = transform.position.y;
transform.position = Vector3.MoveTowards(transform.position, destinationPosition,Time.deltaTime * playerMoveSpeed);
}
}
}