Reputation: 45
I follow this tutorial https://www.youtube.com/watch?v=SrCUO46jcxk
for multitouch, it work. And trying to add drag functionality on each object simultaneously with multiple finger. This is my code for button i add some code to make it draggable each object simultaneously but it work on only single object. I tried all relevant post and tutorial but not able perform, please help??
using System.Collections;
using UnityEngine;
public class Button : MonoBehaviour{
public Color defaultColour;
public Color selectedColour;
private Material mat;
private Vector3 screenPoint;
private Vector3 offset;
void Start(){
mat = GetComponent<Renderer> ().material;
}
void OnTouchDown(){
mat.color = selectedColour;
Debug.Log ("Touch Down");
screenPoint = Camera.main.WorldToScreenPoint (transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnTouchUp(){
mat.color = defaultColour;
Debug.Log ("Touch Up");
}
void OnTouchStay(){
mat.color = selectedColour;
Debug.Log ("Touch Stay");
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
transform.position = curPosition;
}
void OnTouchExit(){
mat.color = defaultColour;
Debug.Log ("Touch Exit");
}
}
This code is for Input Touch functionality
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
void Update () {
#if UNITY_EDITOR
if(Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0)){
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, touchInputMask)){
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if (Input.GetMouseButtonDown(0)){
recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButtonUp(0)){
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (Input.GetMouseButton(0)){
recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
foreach (GameObject g in touchesOld){
if (!touchList.Contains(g)){
g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
#endif
if (Input.touchCount > 0){
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
foreach (Touch touch in Input.touches){
Ray ray = Camera.main.ScreenPointToRay (touch.position);
//RaycastHit hit;
if (Physics.Raycast(ray, out hit, touchInputMask)){
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
if (touch.phase == TouchPhase.Began){
recipient.SendMessage("OnTouchDown",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Ended){
recipient.SendMessage("OnTouchUp",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){
recipient.SendMessage("OnTouchStay",hit.point,SendMessageOptions.DontRequireReceiver);
}
if (touch.phase == TouchPhase.Canceled){
recipient.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
foreach (GameObject g in touchesOld){
if (!touchList.Contains(g)){
g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
Upvotes: 1
Views: 2243
Reputation: 45
Now finally perform this process, i could perform this process in two ways
Using touchScript library, it has a rich feature we can drag, rotate, translate and scale multiple object in one time. https://www.youtube.com/watch?v=HHLBJ1Ss2S0
Note: this is older video, now it changes but it is almost same. [TouchScript is available in asset store for free]
other option, attach below code in empty gameobject or in camera(add tag layer)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public struct objectst { public Vector3 screenPoint; public Vector3 offset; }
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
public static Dictionary<int,objectst> touchobjects = new Dictionary<int,objectst>();
private GameObject[] touchesOld;
private RaycastHit hit;
public Text Count, IndexLift;
private Vector3 targetPos;
void Update () {
int nbTouches = Input.touchCount;
if(nbTouches > 0)
{
nbTouches = 5;
print(nbTouches + " touch(es) detected");
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
for (int i = 0; i < nbTouches ; i++)
{
Touch touch = Input.GetTouch(i);
print("Touch index " + touch.fingerId + " detected at position " + touch.position);
Ray ray = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast(ray, out hit, touchInputMask)){
GameObject recipient = hit.transform.gameObject;
touchList.Add(recipient);
//recipient.;
if (touch.phase == TouchPhase.Began){
objectst tempobj = new objectst ();
tempobj.screenPoint = Camera.main.WorldToScreenPoint (recipient.transform.position);
tempobj.offset = recipient.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (touch.position.x, touch.position.y, tempobj.screenPoint.z));
touchobjects.Add(touch.fingerId,tempobj);
}
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved){
Vector3 curScreenPoint = new Vector3 (touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + touchobjects[touch.fingerId].offset;
recipient.transform.position = curPosition;
}
if (touch.phase == TouchPhase.Ended){
Vector3 curScreenPoint = new Vector3 (touch.position.x, touch.position.y,
touchobjects[touch.fingerId].screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) - touchobjects[touch.fingerId].offset;
recipient.transform.position = curPosition;
}
if (touch.phase == TouchPhase.Canceled){
}
}
}
foreach (GameObject g in touchesOld){
if (!touchList.Contains(g)){
g.SendMessage("OnTouchExit",hit.point,SendMessageOptions.DontRequireReceiver);
}
}
}
}
}
Upvotes: 2