Hi,
How can I convert this to 2D?
I know that I should use vector2D instead? but I am not finding any answers.
What the changed requires to fully making it work on sprite objects with Rigidbody2D?
The below script only works on 3d objects like cubes.
Here’s the script attacked to camera:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TouchInput : MonoBehaviour {
public LayerMask touchInputMask;
private List<GameObject> touchList = new List<GameObject>();
private GameObject[] touchesOld;
private RaycastHit hit;
// Update is called once per frame
void Update () {
if(Input.touchCount>0) {
touchesOld = new GameObject[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear ();
foreach(Touch touch in Input.touches){
Ray ray = Camera.current.ScreenPointToRay(touch.position);
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);
}
}
}
}
}
and here s the script attached to object:
using UnityEngine;
using System.Collections;
public class DestroyByTouch : MonoBehaviour {
// Use this for initialization
void Start() {
}
void OnTouchDown(){
Destroy (this.gameObject);
}
void OnTouchUp(){
Destroy (this.gameObject);
}
void OnTouchStay(){
Destroy (this.gameObject);
}
void OnTouchExit(){
Destroy (this.gameObject);
}
}
But I want to attach it to sprites.