- Home /
How to destroy object by touch?
I tried this code:
if (Input.touchCount > 0)
{
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
var hit: RaycastHit;
if (Physics.Raycast (ray, hit))
{
if (hit.collider.tag == "Enemy")
{
Destroy(hit.transform.gameObject);
}
}
}
But nothing happens when I play my game on the phone. I just want to destroy objects from prefabs when I touch them. They just fall down like raindrops. What should I do?
EDIT: Here's what works (Thank you JeffreyD!):
if (Input.touchCount > 0)
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if(hit.collider != null)
{
Destroy(hit.transform.gameObject);
}
}
Does your game work in windows (or mac) if you swap touch for a mouse click?
Actually I don't know how to do it. Input.Get$$anonymous$$ouseButtonDown(0)
have no position
.
There can be problems if you have multiple overlapping colliders?
hi; raja your code is right but if i want to destroy clone's object ?
Answer by JeffreyD · Feb 12, 2014 at 06:22 PM
I'd do something like... bool myDebug = false; // set to true to see debug info in console void Update() { int fingerCount = 0; RaycastHit hitInfo; Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition); //Debug.DrawRay(transform.position, vfwd * 20, Color.red); foreach (Touch touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) fingerCount++; } if (fingerCount > 0) { if (myDebug) { Debug.Log ("FingerCount is : " + fingerCount); } if (Physics.Raycast(rayOrigin, out hitInfo, 20)) { if (myDebug) { Debug.Log("I see We hit ==> " + hitInfo.transform.name); } if(hitInfo.transform.tag == "Enemy") { if (myDebug) { Debug.Log("We hit the TARGET"); } Destroy(hitInfo.transform.gameObject); // can add time delay as second argument } } // END if raycast } // END if (fingerCount > 0) }
I've not tested this. Let me know if it works. Thanks. P.S. Link to touch info in docs http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html
But what is hit
in Destroy(hit.gameObject);
? I don't know what type this variable should be.
Oops. Just fixed the code. Hit should be hitInfo. Like I said I did not test this. hitInfo contains the gameObject that is "hit" by the raycast, which is "shot" from your figure touch into the gamespace.
Oh, it should be from the finger touch not the mouse. Hmm, I'm using this code for rayCast on a game I'm running on my phone and it works. I'm guessing that the "$$anonymous$$ouse" is your finger touch in this case. I'll have to look into it more into the difference between finger and mouse on $$anonymous$$obile. I'm still figuring out this stuff myself. I saw your post and figured I'd send what was working for me.
Did you try this (with the hitInfo fix)?
In the doc info on touch the 3rd example shows....
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).phase == TouchPhase.Began)
{
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast (ray))
{
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
So, I need to update my own code away from mouse into touch. I'll let you know how it works out. In the mean time, this def of the raycast starting position as the mouse does work so it should be enough for you to try a test. Let me know how you make out.
'gameObject' is not a member of 'UnityEngine.RaycastHit'
You are right. hitInfo.gameObject should be hitInfo.transform.gameObject as you have in your initial post. I fixed the code above.
Answer by raja1250 · Jul 11, 2014 at 04:27 AM
assign this code to the main camera,and drag the game object to cubes variable.and tag the gameobject with "new" tag.Tested in android phone.
using UnityEngine; using System.Collections;
public class touch : 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);
}
}
} }
Your answer
Follow this Question
Related Questions
Avoid touch input when pressing the "Pause" button 1 Answer
OnPointerExit Issue w/ Radial Menu on Android 1 Answer
Expand object on touch? 1 Answer
Android input only works with remote 1 Answer
TouchCount always > 0 on Android 1 Answer