- Home /
How to Change Cameras through a Canvas Button
Currently I can Change Camera by Pressing C from keyboard but I want to do it through a Canvas Button.
Here is the Script:
 public class VehicleCameraController : MonoBehaviour
     {
         public enum Mode
         {
             Fixed,
             SmoothFollow,
             MouseOrbit }
         ;
         public Mode mode = Mode.SmoothFollow;
 
         Mode m_prevMode = Mode.SmoothFollow;
 
                 public KeyCode changeCameraKey = KeyCode.C;
 
         public void LateUpdate ()
         {
             if (Input.GetKeyDown (changeCameraKey)) {
                 if (mode == Mode.MouseOrbit)
                     mode = Mode.Fixed;
             } else {
                 mode++;
             }
 
             if (mode != m_prevMode) {
                 ResetCamera ();
                 m_prevMode = mode;
             }
 
             switch (mode) {
             case Mode.Fixed:
                 
                 DoFixedCamera ();
                 break;
 
             case Mode.SmoothFollow:
                 
                 DoSmoothFollow ();
                 break;
 
             case Mode.MouseOrbit:
                 
                 DoMouseOrbit ();
                 break;
             }
         }
 }
 }
   
Answer by hoogemast · Jul 21, 2016 at 09:32 AM
Isn't it a solution to just create a button that calls a function to change the camera before the update. So in your button onclick there will be called a function changeCamera() that looks almost the same as your current function. So like this.
          public void ChangeCamera()
          {
              if (mode == Mode.MouseOrbit)
                      mode = Mode.Fixed;
              else {
                  mode++;
              }
  
              if (mode != m_prevMode) {
                  ResetCamera ();
                  m_prevMode = mode;
              }
  
              switch (mode) {
              case Mode.Fixed:
                  
                  DoFixedCamera ();
                  break;
  
              case Mode.SmoothFollow:
                  
                  DoSmoothFollow ();
                  break;
  
              case Mode.MouseOrbit:
                  
                  DoMouseOrbit ();
                  break;
              }
          }
And if you really want to develop it in a nice way you can hide the button when the mode is Mode.MouseOrbit .
@hoogemast I have called this function via a Button through onClick method but the problem is that The camera changes but it does not follow the vehicle and remains fixed on its location.
Secondly How can I hide the button when the mode is $$anonymous$$ode.mouseOrbit ?
Thankyou
@omerdanish An very easy way to move the camera with your gameobject is to have the gameobject as a parent of your camera. I dont knnow if that is best practice, but it should work very nice.
I think you could use gameobject.setactive(false) to hide the button. You have to assign the button to a the gameobject through the inspector though.
Answer by omerdanish · Jul 25, 2016 at 09:33 AM
@hoogemast Thankyou for your help I have solved the problem by making this function
 public void  camchange ()
             {
             
             if (mode == Mode.MouseOrbit)
                     mode = Mode.Fixed;
                 else 
                     mode++;
             }
Your answer
 
 
             Follow this Question
Related Questions
Whole Screen Touchable 0 Answers
How can I break an UIText on OnClick of an button without scripts ? 2 Answers
Can't click or highlight a button. 0 Answers
Use UI buttons with Time.timeScale=0? 3 Answers
UI button dosent show function 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                