Raycasting to find mouseclick on Object in unity 2d games

前端 未结 4 1211
猫巷女王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:48

    You cannot use 3D physics functions on the new 2D stuff. Use the 2D functions instead. Example:

    RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    
    if(hit.collider != null)
    {
        Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position);
    }
    

提交回复
热议问题