• 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 Darknuke · Apr 17, 2012 at 06:27 PM · cameraonmousedown

OnMouseDown w/ Multiple Cameras Doesn't Work Right

So I have two cameras. One that is doing a render texture to a target, and one that functions as the main camera. I have OnMouseDown events, in a script that that is attached to every object that they will be called (pieces in a match-3 puzzler), that were working perfectly before adding the second camera, and now they don't work at all. If I put the render target camera over the area where those events would occur, it starts working again.

Clearly the OnMouseDown function is favoring the second camera (render target camera) when checking OnMouseDown events. I don't want this to happen. How do I switch it from second camera to main camera?

I've tried switching the cameras functions around (aka making main the render and vice versa) and it always favors the render target camera. I've tried changing depth, layers, culling masks, and pretty much every other option in the camera editor menu, but to no avail.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gamebytom · Oct 26, 2016 at 12:22 PM

I solved this by using the "MainCamera" tag, i.e.

mainCam.tag="MinorCam"; secondCam.tag="MainCamera";

Comment
Add comment · Show 1 · 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 danilocjr · Aug 10, 2018 at 05:23 AM 0
Share

On 2018 this works for me.

avatar image
3

Answer by Vifate · Aug 02, 2012 at 10:19 AM

I also stumbled upon this bug. I have 2 cameras, one which renders in a texture (TexCam), the other doing the main rendering (MainCam). Here is the workaround I found :

  1. Disable TexCam

  2. Create a C# script :

 public class TexCamHack : MonoBehaviour {
  
     public Camera textureCamera;
  
  void OnPreRender() {
  textureCamera.Render(); 
  }
 }

3. Add the script to MainCam and assign TexCam to the field textureCamera

Comment
Add comment · Show 1 · 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 Vicas · Jan 14, 2014 at 02:45 PM 0
Share

This saved all my problems. I dont have 15 rep so I cant upvote you, but I wish I could. Thanks a lot.

avatar image
0

Answer by Darknuke · Apr 19, 2012 at 05:31 PM

I ended up changing the code in my master script to do this, which essentially ports all the OnMouse events to manual raycasting.

 camRay = boardCamera.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(camRay, out hit, 100))
     {
         if(hit.collider.gameObject.layer == 8)
         {
             currentPiece = hit.collider.GetComponent<PieceScript>();
             if(currentPiece != lastPiece)
                 currentPiece.MouseEnter();
             if(currentPiece != lastPiece && lastPiece != null)
             {
                 lastPiece.MouseExit();
             }
             lastPiece = currentPiece;
             if(Input.GetMouseButton(0))
                 currentPiece.MouseDrag();
             if(Input.GetMouseButtonDown(0))
                 currentPiece.MouseDown();
             if(Input.GetMouseButtonUp(0))
                 currentPiece.MouseUp();
         }
     }
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
avatar image
0

Answer by programmrzinc · Apr 17, 2012 at 10:00 PM

var CAM1 : GameObject;

car CAM2 : GAmeObject;

function OnMouseDown () {

  CAM1.// blah blah
 
  CAM2.//Blah Blah
 }

Attach this to the rendered object that you want to click

Comment
Add comment · Show 3 · 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 Darknuke · Apr 17, 2012 at 10:04 PM 0
Share

I'm using C#, but kind of get what you are saying.

Does the On$$anonymous$$ouseDown event loop through cameras? Aka, I can do an if(cam1) then ...?

avatar image syclamoth · Apr 17, 2012 at 11:46 PM 0
Share

On$$anonymous$$ouseDown works on a per-frame basis. Every camera will render on every frame, so if you are s$$anonymous$$ling the events with one camera, it will prevent the others from responding to the clicks. Try restructuring so that all the clicks happen on one 'master' camera.

Alternatively, try removing all the non-camera components from the second camera? $$anonymous$$aybe that'd work.

In any case, you can always just switch to manual raycasting ins$$anonymous$$d of using the On$$anonymous$$ouseDown event, which would remove the problem entirely (by allowing you to choose which camera the raycasts work with).

avatar image Darknuke · Apr 19, 2012 at 04:08 PM 0
Share

I don't want to have to rewrite if I don't need to (I'm using code from the asset store, so I don't want to risk breaking it). This just seemed like such a simple problem at first.

So I removed the "render texture" from the second camera and it caused both cameras to register onmousedown events perfectly. Why do render textures cause only the render textured camera to register mouse events? That kind of seems like a poor usability bug on Unity's part.

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

9 People are following this question.

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

Related Questions

Simple Camera Movement?! (javascript) 1 Answer

OnMouseDown and OnMouseUp not working,onMouseDown not working 0 Answers

OnMouseDown on what? Camera Movement Help. 1 Answer

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

How to make camera position relative to a specific target. 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