• 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
1
Question by Diego89 · Jun 05, 2013 at 10:01 AM · cameraonmousedown

OnMouseDown with two camera, how can I make it work?

I have an object, this object is just a box, with a box collider and a rigidbody. Attached to the cube there is a simple script

function OnMouseDown() { Debug.Log("I'm working"); // code }

however when I click on the cube I don't see the message. I make some research and I discovered that OnMouseDown is not working well when there are two camera in the scene.

I have two camera; the first one is the main camera and is attached to the first person controller, the second one is a text camera.

Depth only HUD Text Orthographic

this problem is creating a lot of issue. I don't know hot to solve it; there is anyone with some idea ?

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
Best Answer

Answer by robertbu · Jun 06, 2013 at 03:40 AM

One way to solve the issue is to go from OnMouseDown() to using Raycasting to detect a hit. It is hard for me to suggest specifics with so little knowledge of what you do inside your OnMouseDown(). The typical structure would be:

 function Update() {
 
     if (Input.GetMouseButtonDown(0)) {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         var hit : RaycastHit;
         if (Physics.Raycast(ray, hit)) {
             if (hit.collider.tag == "some_tag") {
                 // Do something
             }
         }
     }
 }

This would be attached to a single game object. The code above uses "Camera.main," to create the ray (the camera tagged as MainCamera), but you can use whatever camera you want. This code uses the "tag" of the game object hit, but you can look for a specific script instead:

 Clickable clickable = hit.collider.gameObject.GetComponent(Clickable);
 if (clickable != null)
    clickable.MyMouseDown()
Comment
Add comment · Show 2 · 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
avatar image Diego89 · Jun 06, 2013 at 07:56 AM 0
Share

thank you that worked perfectly

avatar image danilocjr · Aug 10, 2018 at 05:25 AM 0
Share

Before use this, make pretty sure that you have 2 cameras tagged differently. If you have 2 'mainCameras', yes, you need a script to do it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers

How to make camera position relative to a specific target. 1 Answer

How to get the camera responsible for OnMouseDown()? 1 Answer

Mouse scroller problem 1 Answer

Camera movement(I have no clue how to code) 0 Answers


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