Raycasting to find mouseclick on Object in unity 2d games

前端 未结 4 1369
猫巷女王i
猫巷女王i 2021-02-04 13:10

I am trying to delete the object on which the mouse is clicked. I am making a 2D game using the new Unity3D 4.3. Here is the code I\'m using

void Update () {

           


        
4条回答
  •  忘掉有多难
    2021-02-04 13:47

    You've to attach a mesh collider(any collider) with your object first to enter the inner If. Then,

    Destroy(hit.collider.gameObject); 
    

    will simply do the job.

    There's might be an other work around here.

    void Update () {
    
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
    
        if(Physics.Raycast(ray,out hit))
        {
            if(Input.GetMouseButtonDown(0))
            {
                isHit = false;
                Destroy(hit.collider.gameObject);
            }
        }
    }
    

提交回复
热议问题