C# 使用Physics.Raycast和Physics2D.Raycast检测对象上的单击

C# 使用Physics.Raycast和Physics2D.Raycast检测对象上的单击,c#,unity3d,mouseevent,C#,Unity3d,Mouseevent,我有一个空的游戏对象在我的场景与组件盒碰撞2D 我将脚本附加到此游戏对象,其中包含: void OnMouseDown() { Debug.Log("clic"); } 但是当我点击我的游戏对象时,没有效果。你有什么想法吗?如何检测我的长方体碰撞器上的单击?使用光线投射。检查是否按下了鼠标左键。如果是,请将不可见光线从鼠标单击发生的位置投射到发生碰撞的位置。 对于三维对象,请使用: 3D模型: void check3DObjectClicked () { if (Input.G

我有一个空的游戏对象在我的场景与组件盒碰撞2D

我将脚本附加到此游戏对象,其中包含:

void OnMouseDown()
{
    Debug.Log("clic");
}

但是当我点击我的游戏对象时,没有效果。你有什么想法吗?如何检测我的长方体碰撞器上的单击?

使用光线投射。检查是否按下了鼠标左键。如果是,请将不可见光线从鼠标单击发生的位置投射到发生碰撞的位置。 对于三维对象,请使用:

3D模型:

void check3DObjectClicked ()
{
    if (Input.GetMouseButtonDown (0)) {
        Debug.Log ("Mouse is pressed down");
    
        RaycastHit hitInfo = new RaycastHit ();
        if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo)) {
            Debug.Log ("Object Hit is " + hitInfo.collider.gameObject.name);

            //If you want it to only detect some certain game object it hits, you can do that here
            if (hitInfo.collider.gameObject.CompareTag ("Dog")) {
                Debug.Log ("Dog hit");
                //do something to dog here
            } else if (hitInfo.collider.gameObject.CompareTag ("Cat")) {
                Debug.Log ("Cat hit");
                //do something to cat here
            }
        } 
    } 
}

2D精灵:

void check3DObjectClicked ()
{
    if (Input.GetMouseButtonDown (0)) {
        Debug.Log ("Mouse is pressed down");
    
        RaycastHit hitInfo = new RaycastHit ();
        if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo)) {
            Debug.Log ("Object Hit is " + hitInfo.collider.gameObject.name);

            //If you want it to only detect some certain game object it hits, you can do that here
            if (hitInfo.collider.gameObject.CompareTag ("Dog")) {
                Debug.Log ("Dog hit");
                //do something to dog here
            } else if (hitInfo.collider.gameObject.CompareTag ("Cat")) {
                Debug.Log ("Cat hit");
                //do something to cat here
            }
        } 
    } 
}
上述解决方案适用于3D。如果希望它适用于2D,请将Physics.Raycast替换为Physics2D.Raycast。例如:

void check2DObjectClicked()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("Mouse is pressed down");
        Camera cam = Camera.main;

        //Raycast depends on camera projection mode
        Vector2 origin = Vector2.zero;
        Vector2 dir = Vector2.zero;

        if (cam.orthographic)
        {
            origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            origin = ray.origin;
            dir = ray.direction;
        }

        RaycastHit2D hit = Physics2D.Raycast(origin, dir);

        //Check if we hit anything
        if (hit)
        {
            Debug.Log("We hit " + hit.collider.name);
        }
    }
}

使用光线投射。检查是否按下了鼠标左键。如果是,请将不可见光线从鼠标单击发生的位置投射到发生碰撞的位置。 对于三维对象,请使用:

3D模型:

void check3DObjectClicked ()
{
    if (Input.GetMouseButtonDown (0)) {
        Debug.Log ("Mouse is pressed down");
    
        RaycastHit hitInfo = new RaycastHit ();
        if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo)) {
            Debug.Log ("Object Hit is " + hitInfo.collider.gameObject.name);

            //If you want it to only detect some certain game object it hits, you can do that here
            if (hitInfo.collider.gameObject.CompareTag ("Dog")) {
                Debug.Log ("Dog hit");
                //do something to dog here
            } else if (hitInfo.collider.gameObject.CompareTag ("Cat")) {
                Debug.Log ("Cat hit");
                //do something to cat here
            }
        } 
    } 
}

2D精灵:

void check3DObjectClicked ()
{
    if (Input.GetMouseButtonDown (0)) {
        Debug.Log ("Mouse is pressed down");
    
        RaycastHit hitInfo = new RaycastHit ();
        if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hitInfo)) {
            Debug.Log ("Object Hit is " + hitInfo.collider.gameObject.name);

            //If you want it to only detect some certain game object it hits, you can do that here
            if (hitInfo.collider.gameObject.CompareTag ("Dog")) {
                Debug.Log ("Dog hit");
                //do something to dog here
            } else if (hitInfo.collider.gameObject.CompareTag ("Cat")) {
                Debug.Log ("Cat hit");
                //do something to cat here
            }
        } 
    } 
}
上述解决方案适用于3D。如果希望它适用于2D,请将Physics.Raycast替换为Physics2D.Raycast。例如:

void check2DObjectClicked()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("Mouse is pressed down");
        Camera cam = Camera.main;

        //Raycast depends on camera projection mode
        Vector2 origin = Vector2.zero;
        Vector2 dir = Vector2.zero;

        if (cam.orthographic)
        {
            origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        else
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            origin = ray.origin;
            dir = ray.direction;
        }

        RaycastHit2D hit = Physics2D.Raycast(origin, dir);

        //Check if we hit anything
        if (hit)
        {
            Debug.Log("We hit " + hit.collider.name);
        }
    }
}

我把你的代码放在更新功能中,但如果我点击我的游戏对象,我只会看到“鼠标被按下”的消息。你是否在游戏对象上附加了碰撞器?你是否从unity 4.xxx升级了你的项目,或者这是在5中创建的新项目?这是一个新项目。我只有一个空的游戏对象和2d盒对撞机,一个脚本与你的功能。。你也一样吗?让我们看看。我把你的代码放在更新功能中,但如果我点击我的游戏对象,我只有“鼠标被按下”的信息。你是否在游戏对象上附加了碰撞器?你是否从unity 4.xxx升级了你的项目,或者这是在5中创建的新项目?这是一个新项目。我只有一个空的游戏对象和2d盒对撞机,一个脚本与你的功能。。你也一样吗?让我们来。