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 androids · Feb 23, 2015 at 12:03 PM · camerarotationposition

Switch between Camera's

I have 2 camera's:

  • Camera01

  • Camera02

I have also this JS:

 var Camera01 : Camera;
 var Camera02 : Camera;
 var Player : Transform;
 var Switcher : boolean;
 var Timer : float = 10;
 var ReduceTimer : boolean;
 
 function Update (){
    if (Input.GetMouseButtonDown (0)){
       Switcher = true;
       Timer = 10;
    }
 
    if (Switcher == true){
       ReduceTimer = true;
       Camera01.transform.position = Camera02.transform.position;
       Camera01.transform.rotation = Camera02.transform.rotation;
       Player.transform.rotation.y = Camera01.transform.rotation.y;
    }
    else{
       Camera02.transform.position = Camera01.transform.position;
       Camera02.transform.rotation = Camera01.transform.rotation;
    }
 
    if (ReduceTimer == true){
       Timer -= 1 * Time.deltaTime;
    }
     
    if (Timer <= 0){
       Switcher = false;
       ReduceTimer = false;
    }
 }

Everything works, but the problem is, that the Camera's have the same rotation and position as when I changed the camera's.

By example,

Camera01 (position = 0, 10, 0)(rotation = 0, 90, 0)

Camera02 = disabled

Left Mouse button

Camera02 (position = 10, 90, 0)(rotation = 45, 0, 0)

Camera01 = disabled

What I want is, if I switch the camera's from Camera01 to Camera02, I want that Camera02 have the same rotation and position as Camera01 (I can rotate the camera) and if I switch back, Camera02 have the new position and rotation of Camera01. etc

Hope you guys understand my question, sorry for the difficult explanation.

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

2 Replies

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

Answer by androids · Feb 24, 2015 at 10:35 PM

After hours of scripting, I finally got a better solution. Everyone who have the same problem I had, here is a whole Javascript you can use for Your own project!

 var target : Transform;
 
 var distance = 6;
 var distanceMin = 3;
 var distanceMax = 6;
 
 var yMinLimit = -16;
 var yMaxLimit = 50;
 
 var xSpeed = 13;
 var ySpeed = 13;
 
 var Switcher : boolean;
 
 private var x = 0.0;
 private var y = 0.0;
 
 function Start ()
 {
     var angles = transform.eulerAngles;
     x = angles.y;
     y = angles.x;
 }
 
 function Update ()
 {
     //This is a test key to see if everything is working
     if (Input.GetKeyDown ("c"))
     {
         Switcher = !Switcher;
     }
 }
 
 function LateUpdate ()
 {
     if (target)
     {
         if (Switcher == true)
         {
             x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.2;
             y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.2;
         }
         
         if (Input.GetMouseButton (0) && Switcher == false)
         {
             x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.2;
             y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.2;
         }
             
             y = ClampAngle (y, yMinLimit, yMaxLimit);
                     
             var rotation = Quaternion.Euler (y, x, 0);
             var position = rotation * Vector3 (1.0, 0.0, -distance) + target.position;
             
             var hit : RaycastHit;
             if (Physics.Linecast (target.position, transform.position, hit))
             {
                 distance -= hit.distance;
             }
             
         if (Switcher == true)
         {
             position = rotation * Vector3 (1.0, 1.9, -distance) + target.position;
             distance = 3;
             yMinLimit = -30;
             yMaxLimit = 30;
         }
             else
         {
             position = rotation * Vector3 (0.0, 1.9, -distance) + target.position;
             distance = 6;
             yMinLimit = -16;
             yMaxLimit = 50;
         }
     
         transform.rotation = rotation;
         transform.position = position;
     }
 }
 
 static function ClampAngle (angle : float, min : float, max : float)
 {
     if (angle < -360)
         angle += 360;
     if (angle > 360)
         angle -= 360;
     return Mathf.Clamp (angle, min, max);
 }

Here you go!

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 siaran · Feb 24, 2015 at 12:11 AM

You need to keep track of which camera is active. If you only have 2 camera's you can do that with a boolean, if you have more you should make an enum or something.

then you'd make a method like

 //This keeps track of which camera is active
 bool cameraTracker = true;

     function SwitchCamera(){
      //Go from Camera1 to Camera2
      if(cameraTracker){
       Camera2.transform.position = Camera1.transform.position;
       Camera2.transform.rotation = Camera1.transform.rotation;
       Camera2.enabled = true;
       Camera1.enabled = false;
       cameraTracker = false;
      }
     //Go from Camera2 to Camera1
      else{
       Camera1.transform.position = Camera2.transform.position;
       Camera1.transform.rotation = Camera2.transform.rotation;
       Camera1.enabled = true;
       Camera2.enabled = false;
       cameraTracker = true;
      }
  }

I usually do C# and there are other problems with that code, but you get the idea... At least I think that is what you want?

Or do you mean that when you switch camera you want the other one to reset to it's initial position? In that case, store it's initial positions and set it back to those when you switch.

Edit: also change camera tracker bool on switch.

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 androids · Feb 24, 2015 at 12:56 AM 0
Share

hmmm it's doesn't work or I just have no idea how to fix the js. By the way, it's all about 2 camera's. Please help

avatar image siaran · Feb 24, 2015 at 12:57 AM 0
Share

eh, I forgot to change the cameraTracker variable in that snippet when I change camera. Changed things around a bit so it makes more sense.

avatar image androids · Feb 24, 2015 at 01:18 AM 0
Share

Nope, it's not working. This freaks me out. I'm trying to make an "OrbitCamera" arround the player and an "AimCamera" to fire. But still no succes.. have you any ideas?

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

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

Related Questions

How to freeze the rotation or the position on the camera transform? 1 Answer

Need help on the 3ds max style camera control 0 Answers

How to move camera up and down 1 Answer

Mouse.position from center of player 1 Answer

if (transform.rotation == Vector3(0,0,0)) doesn't work! 2 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