Detecting A MouseClick OutSide Of An Object

Hello i’ve been trying to detect when the mouse is clicked outside of an object, i’ve tried raycasting but i cant seem to find how to make it work, and i’ve also tried some OnMouseDown functions but i didn’t work, i’ve been looking for an answear for about 3 hours now, and im really tired and now im asking if you guys have any ideas?

Do you mean that you’re not managing to detect whether the click hits nothing or whether it simply doesn’t hit the same object again?

Depending on what you’re aiming for, here’s how you can approach [either][1]:

// C#

GameObject lastHit; // Keep track of last object clicked

void Update()
{
    RaycastHit hit;
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if(Physics.Raycast(ray, out hit, 100)) // or whatever range, if applicable
    {
        if(hit.transform.gameObject == lastHit)
        {
            // You clicked the same object twice
        }
        else
        {
            lastHit = hit.transform.gameObject;
        }
    }
    else
    {
        // You didn't click on anything.
        // Either out of range or empty skybox behind mouse cursor
    }
}

Hope this helps you out!

Edit: In case you want it to be more lenient about whether you clicked an object behind another object, you could turn RaycastHit into RaycastHit and use [RaycastAll()][2] instead to determine whether the object was there.

// ...
RaycastHit[] hits = Physics.RaycastAll(ray, 100);
// ...
for(int i = 0; i < hits.Length; i++)
{
    if(hits*.gameObject == lastHit)*

{
// Match!
}
// …
}
// …
Edit: Adding quick translations to the scripts…
// Javascript

var lastHit: GameObject; // Keep track of last object clicked
function Update()
{
var hit: RaycastHit;
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit, 100)) // or whatever range, if applicable
{
if(hit.transform.gameObject == lastHit)
{
// You clicked the same object twice
}
else
{
lastHit = hit.transform.gameObject;
}
}
else
{
// You didn’t click on anything.
// Either out of range or empty skybox behind mouse cursor
}
}
and…
// …
var hits: RaycastHit[] = Physics.RaycastAll(ray, 100);
// …
for(var i = 0; i < hits.Length; i++)
{
if(hits*.gameObject == lastHit)*
{
// Match!
}
// …
}
// …
I didn’t care because the conversions were cake, but if you notice, I only changed a few lines to switch from C# to Javascript for either case.
_[1]: http://docs.unity3d.com/ScriptReference/Physics.Raycast.html*_
_
[2]: http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html*_

Scale the collider ,with screen width and hight and now it shoud work fine it doesen’t matter where you point you can do something when you click this is not the best way to do it i don’t know why you want to do this you must be more accured and sorry for my bad english i hope you understand something :smiley:

First you need to put your objects in different layers. Like all walls in a walls layer. And so on.

Then you can use that layers an tell the raycast to ignore layers or compare which layer got hit and react to it.

So lets cast a Ray:

public LayerMask SellMask;

    public void Update () 
    {
    	if(Input.GetMouseButton(0))
    	{
    		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    		RaycastHit hit;
    		
    		if(Physics.Raycast(ray, out hit, 100, SellMask))
    		{
    			//here you can react
    			//i use a temp var here so i can use it in other statements
    			var sellTemp = hit.transform.gameObject;//the hitted gameobject is stored
    			Destroy(sellTemp);//and here i can do all like destroy it
    		}
    	}
    }

I hope that basic overview helps you out.