Search code examples
androidunity-game-enginekudan

Kudan Unity :Destroying GameObject on Touch


I am using the Kudan plugin in Unity for building an android app. I am using the markerless mode. When I tap (touch) model, which spawns in this markerless mode, I want to destroy it. I am using a Raycast script as listed below. I tried attaching the script to Kudan Camera. Also I created a public Camera gameobject and passed the Kudan Camera to it. However neither option works.

Does anyone know what I am doing wrong?

public Gamobject model;

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 == "chair") 
            {
                Destroy (model); 
            }
        }
    }  

Thanks in advance!


Solution

  • Assuming that your actual code is correct and says GameObject instead of Gamobject and your if statement has an opening brace, etc, it could be that:

    • You haven't given your model the "chair" tag. If your ray hits the model, but the model doesn't have the right tag, it will be ignored.
    • The Ray isn't going far enough to hit anything (as mentioned in comments). You haven't specified a distance, which in theory should mean that there isn't a maximum distance, but you never know, Unity might have some weird default or something.
    • Your model doesn't have a collider. Objects need a collider otherwise the physics engine can't register the "collision" between the ray and the object.

    But really, an easier and probably better method would be to not use a Raycast at all. If you make a method in a script:

    void OnMouseDown
    {
        Destroy(gameObject);
    }
    

    and attach that script to your model, then when the user clicks on the model (or taps on mobile), provided it has a collider, it will be destroyed.