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 adrianbsk · Feb 16, 2014 at 04:00 AM · cameraerrorscaleswitchswipe

i need help with this camera switcher

i need help with a script who when i playing and i stay in a range i can activate with a key a camera switcher this is the script, but this script not work, pleaSe help me!!

 Vector3 camPosition;
 Vector3 myPosition;
  
 int range = 10;
  
 if(Vector3.Distance(myPosition, camPosition) < range)
 {
      if(Input.GetKeyDown("1"))
      {
           cam1.enabled = true;
           cam2.enabled = false;
      }
      if(Input.GetKeyDown("2")){
           cam1.enabled = false;
           cam2.enabled = true;
      }
 }
 
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Feb 17, 2014 at 07:38 AM

There are a lot of unknowns about what you want to do here, and there is either code missing, or you don't understand how to structure C# classes. Here is something that compiles that may be in the direction of where you are heading:

 using UnityEngine;
 using System.Collections;
 
 public class Switcher : MonoBehaviour {
     
     public Vector3 camPosition;
 
     public Camera cam1;
     public Camera cam2;
 
     float range = 10.0f;
 
     void Update() {
         if(Vector3.Distance(transform.position, camPosition) < range)
         {
             if(Input.GetKeyDown("1"))
             {
                 cam1.enabled = true;
                 cam2.enabled = false;
             }
             if(Input.GetKeyDown("2")){
                 cam1.enabled = false;
                 cam2.enabled = true;
             }
         }
     }
 }

'cam1' and 'cam2' need to be initialized in the Inspector by dragging the two cameras onto the variable, and 'camPosition' needs to be assigned in the inspector. I'm not sure what 'camPosition' represents. If it is a reference to the position of one of the cameras, then the code should handle things a bit differently.

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 HarryGodden · Feb 17, 2014 at 12:16 PM

 public Vector3 camPosition;
 Vector3 myPosition;
 
 public gameObject cam1;
 public gameObject cam2;
 
 int range = 10;`

 void Start(){
      cam2.SetActive(false);
 }
 
 void Update(){
 
      if(Vector3.Distance(myPosition, camPosition) < range){
      
           if(Input.GetKeyDown(keycode.Alpha1){
                cam1.SetActive(false);
                cam2.SetActive(true);
           {
           if(Input.GetKeyDown(keycode.Alpha2){
                cam1.SetActive(true);
                cam2.SetActive(false);
           {
 
      
      }
 
 }

this should work. you need to assign the camPosition in the Inspector window inside unity. and the cam1 and cam2 also need to be assigned in the inspector. After all the public variables have been assigned then this script should be good to go!

hope this helps you -Harry

this script is written in C#

ignore half the script going green, don't know what happened there XD

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 adrianbsk · Feb 17, 2014 at 06:44 PM

THANKS all i go to test the scripts!!! :D

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

19 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 avatar image avatar image avatar image avatar image

Related Questions

Switching cameras? 1 Answer

Cinemachine LensSettings.cs Errors? 0 Answers

auto scale camera error when upgrading unity4.3 into 4.5 0 Answers

Keep line renderer width same on screen no matter camera distance 1 Answer

character switching on collision 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