进行射线投射以在统一2D游戏中找到对对象的鼠标单击 [英] Raycasting to find mouseclick on Object in unity 2d games

查看:183
本文介绍了进行射线投射以在统一2D游戏中找到对对象的鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除单击鼠标的对象.我正在使用新的Unity3D 4.3制作2D游戏.这是我正在使用的代码

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 () {

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

        }
    }

}

该控件未进入内部if循环. (未将isHit设置为false).

The control is not entering the inner if loop. (isHit is not being set as false).

推荐答案

您不能在新的2D材质上使用3D物理功能.请使用 2D函数.示例:

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);
}

这篇关于进行射线投射以在统一2D游戏中找到对对象的鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆