Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DosaSzabolcs · Jul 18, 2015 at 03:47 PM · c#cameracollidercontroller

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
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".

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges