- Home /
Destroy Object On Touch?
Hello, so I needed a script for destroying an object when it is touched on. This game is 2D and for the android. The last script I used was this -
using UnityEngine; using System.Collections;
public class Break : MonoBehaviour {
public GameObject cubes;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount >0)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
if (hit.collider.gameObject.tag =="new")
{
Destroy(cubes);
//cubes.rigidbody.AddForce(Vector3.forward * Time.deltaTime *500);
}
}
}
}
I don't understand why it isn't working. I've changed the tag of the object to new and it still won't work. Any help please?
I bet it's yet another problem with 2D and 3D objects... Are you sure your objects have 3D-colliders? If they have 2D-colliders your raycasting will hit nothing.
Have you look into TouchPhase? Unity needs to know if you want a simple touch on the screen or some sort of movement.
therefore, you should write it as:
if (input.touchCount > 0 && input.GetTouch(0).phase == TouchPhase.Began){ //your codes.....}
@JChiu, by the way, your code will only check if the first touch is just began, ignoring all other touches.
Then try this:
if (Input.touchCount >0)
{
Debug.Log("Touch detected.");
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Raycast hit " + hit.collider.name);
if (hit.collider.gameObject.tag =="new")
{
Debug.Log("The tag matches.");
Destroy(cubes);
}
}
}
Answer by meat5000 · Feb 09, 2015 at 02:33 AM
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
2D game you say?
Answer by chariot · Feb 10, 2015 at 08:13 AM
Just if u dont use multitouch :)
private var mouseEntered : boolean;
function Start () {
// ur code here
}
function Update (){
// ur code here
if (mouseEntered && Input.GetMouseButtonDown(0)){
Destroy(gameObject);
}
}
function OnMouseEnter(){
mouseEntered = true;
}
function OnMouseExit(){
mouseEntered = false;
}
This code works fine also for mobile platforms (thx for default converting mouse handler functions by Unity), but only for one-touch apps.
Answer by ASECENAS · Feb 01, 2017 at 09:06 AM
Hi guys, I´m new in this... and I used kind the same code... but I want to measure the time of every touch of an object... it works fine with one... but it doesn´t measure the second touch so it can be destroy in certain time. here is the code:
void Update () {
int nbTouches = Input.touchCount;
if (nbTouches > 0)
{
print(nbTouches + " touch(es) detected");
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
ray = Camera.main.ScreenPointToRay(touch.position);
print("Touch index " + touch.fingerId);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.tag == "Circle")
{
touchTime = Time.fixedTime;
Debug.Log("TIME " + touchTime);
if (touchTime > holdTime)
{
touchTime = 0;
Debug.Log("Destroy Object in " + touchTime);
Destroy(hit.collider.gameObject, lifeTime);
}
}
}
}
}
}