Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BillyGlenn · Jan 09, 2011 at 01:39 PM · camera

Switch camera with mouse

hi,

im currently using this to switch cameras

var cam1 : Camera;

var cam2 : Camera;

function Update () {

  if(Input.GetKeyDown("1")){

       cam1.enabled = true;

       cam2.enabled = false;
  }
  if(Input.GetKeyDown("2")){

       cam1.enabled = false;

       cam2.enabled = true;
  }

}

But instead of pressing a button, i want to change camera when i click on an object with the left mouse button

Comment
Add comment · Show 2
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 The_r0nin · Jan 09, 2011 at 01:57 PM 0
Share

Then you'll need to look up Raycast and ScreenPointToRay in the scripting reference, put tags and colliders on your objects, and then Raycast where you mouseclick and read the tag of that object. Then you can select the camera based on the object tags. There are answers here on this site for each of these individual steps, so there's no need to recapitulate all of them...

avatar image The_r0nin · Jan 09, 2011 at 01:59 PM 0
Share

Or you can use On$$anonymous$$ouseDown...

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by The_r0nin · Jan 09, 2011 at 02:01 PM

Try OnMouseDown on your object scripts.

In the camera script:

var helperObject: GameObject; // assign this in the inspector

function OnMouseDown () { helperObject.SwitchCam(1); // or 2 or 3... whatever the number }

On your helper object's script:

var cam1:Camera; // assign these in inspector var cam2:Camera; var cam3:Camera;

function SwitchCam(newCam:int) { cam1.enabled = false; cam2.enabled = false; cam3.enabled = false; switch (newCam) { case 2: cam2.enabled = true; break; case 3: cam3.enabled = true; break; default: cam1.enabled = true; break; } }

Now, there are other good coding practices (like using enums instead of numbers, etc) that you can include, but this should get you started.

Comment
Add comment · Show 9 · 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 BillyGlenn · Jan 09, 2011 at 08:18 PM 0
Share

thanks for help but i thing im doing something wrong. Im getting a "expecting EOF, found helperObject" and a "expecting EOF, found cam1"

avatar image The_r0nin · Jan 09, 2011 at 08:39 PM 0
Share

$$anonymous$$y bad, forgot I had switched to javascript for this. Try my newest edit...

avatar image BillyGlenn · Jan 10, 2011 at 07:16 AM 0
Share

BCE0019 switchcam is not a member of `unityengine.Gameobject

avatar image The_r0nin · Jan 10, 2011 at 10:58 AM 0
Share

Capitalization matters in Unity script; case is important. It has to be "SwitchCam", not "switchcam." Functions start with a capital letter, and variables with a lower case letter (usually).

avatar image BillyGlenn · Jan 10, 2011 at 03:11 PM 0
Share

sorry its "BCE0019 SwitchCam is not a member of `unityengine.Gameobject" i copied and pasted it directly from your comment.

Show more comments
avatar image
-1

Answer by BillyGlenn · Jan 09, 2011 at 02:52 PM

can i insert the function OnMouseDown in the camera script? I want to remove the second button and then give every object his own script. Something like:

Object 1:

var cam1 : Camera;

var cam2 : Camera;

function Update () {

if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {

cam1.enabled = true;

cam2.enabled = false;

}

Object 2:

var cam1 : Camera;

var cam2 : Camera;

function Update () {

if(Input.GetKeyDown("1")){ >>>>Now here the function OnMouseDown () {

cam1.enabled = false;

cam2.enabled = true;

}

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 The_r0nin · Jan 09, 2011 at 06:03 PM 0
Share

No, the On$$anonymous$$ouseDown goes on the objects themselves. What you do is put a function in script called SwitchCam on an empty object and cache that empty object in your camera scripts. I'll edit my post to show you.

P.S., please don't use answers to comment on someone else's answers... that's not really their purpose. Use the comments section...

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

No one has followed this question yet.

Related Questions

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

Wrong camera position/orientation on windows 10 store build: How to fix it? 0 Answers

Object not centering with screen.width/2 0 Answers

Setting camera to terrain generated billboards 0 Answers

Input.mousePosition not updating? 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