- Home /
Destroy on touch for iphone
When I'm trying to destroy an object when its clicked I simply use on mouse down destroy game object but When I port to iphone that 1. doesn't work cause of mouse commands but then 2. I fix it to use touch and instead of when I click the object it destroys it, it actually will simply destroy the object no matter where I click (touch).
Here is the code but the desired effect is when I touch that object it destroys itself...
#pragma strict var objectToKill : GameObject;
function Update(){
var hit : RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 100)) {
}
if (Input.GetMouseButtonDown(0)){
if (hit.collider.name == objectToKill)
{
Destroy(gameObject);
}
}
}
Also, Input.Get$$anonymous$$ouseButtonDown does convert to iphone fine other scrips I have use it. but in this case I did change it to iPhoneInput.touchCount == 1 which also works.
I must know, did you place this code dirrectly on the target of destruction or on the camera?
Answer by Earth-O-Matic · Apr 18, 2011 at 06:11 AM
I figured it out. Here is what I ended up doing if any one else is in a similar situation. This works on iphone. Probably don't need to specify tag. You could probably just use gameObject.
#pragma strict
function Update () {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.gameObject.tag == "tagOfYourObect")
{
Destroy(hit.collider.gameObject);
}
}
}
}
Your answer
Follow this Question
Related Questions
Destroy object using mouse click(raycast to detect collision) 2 Answers
Android touch 3d Object event 1 Answer
I want to destroy the top object from a stack of objects on touch, not all objects. 1 Answer
Can't seem to destroy instantiated objects. 1 Answer
Destroy objects by clicking on them 1 Answer