- Home /
Detecting if background is clicked
I'm trying to make a room where you can click and drag to pan the camera, when you click an arrow it will move the camera to another room. The problem is that when I click on the arrow it also launches the function from my Camera Controller that is listening for Input.GetMouseButton(0).
I would like to detect somehow that there were no collider hit and only then should the camera pan. I want this to be general rule, if any collider is clicked don't run the pan camera script.
This is the current control that moves the camera :
void Update () {
if(Input.GetMouseButtonDown(0))
TapPos = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, 0 , 0));
if(Input.GetMouseButton(0))
MouseDragStart();
}
void MouseDragStart (){
if (currentRoom.GetComponent<BgProperties> ().Scrollable) {
Vector3 _newPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, 0, 0));
transform.position = new Vector3 (Mathf.Clamp (transform.position.x + (TapPos.x - _newPos.x), LeftLimit, RightLimit), transform.position.y, transform.position.z);
TapPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, 0, 0));
}
}
Answer by AlwaysSunny · Jul 18, 2015 at 03:46 PM
Format code with 101010 button or your questions will be rejected.
There's a pretty easy way to do this if all the things you can "click on" are confined to a canvas. Just put a big invisible Image element across the whole canvas, behind all other elements, and read its pointer enter/exit events to determine whether you can invoke the camera function with LMB.
Otherwise, my preferred solution would probably be confining all mouse-related logic to a single script, so that you have coherent, explicit control over all mouse-related events. You'd replace any existing mouse-related logic with logic run through this "InputManager" script.
The hit object(s) themselves determine what each input event will do. e.g. if I have ABC script, clicking while I'm under the cursor does XYZ
You could also call logic on scripts on those objects if that's your preference. e.g. if the mouse ray hit an object with the IClickable interface, invoke its OnClick event.
With this approach, most mouse events should probably be consumed by whatever was clicked (perhaps by return-ing execution after getting handled). So the last event in the control flow would be manipulating the camera: if no other mouse event was handled, you know you didn't click anything clickable, and are thus clicking "the background".
Your answer
![](https://koobas.hobune.stream/wayback/20220611231735im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera Follow Player using a path 0 Answers
Distribute terrain in zones 3 Answers
How can i make a FF Crystal Chronicles Ring of Fates Controller in Unity ? (Final fantasy) 0 Answers
3 person controler. rotate and turn 0 Answers
Multiple Cars not working 1 Answer