Search Unity

Camera Switching and animation in FSM

Discussion in 'Scripting' started by cristo, Feb 7, 2016.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I'm trying to manipulate a camera to move towards a transform, have an animation play, and when the animation is over (isPlaying), for the camera to be disabled and the first person controller to be enabled.

    Any feedback would be great.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Level1Test : MonoBehaviour
    5. {
    6.     public GameObject Camera;
    7.     public GameObject Transformm;
    8.     public GameObject FPSController;
    9.     public float moveSpeed;
    10.  
    11.  
    12.    
    13.  
    14.     public enum State
    15.     {
    16.         Zoom,
    17.         Anim,
    18.         CamSwitch
    19.     }
    20.  
    21.     public State _state;
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.         Camera.active = true;
    27.         Transformm.GetComponent<Animation>().Play("Ttransform");
    28.  
    29.  
    30.     }
    31.  
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         if (Camera.active = true)
    37.         {
    38.             _state = State.Zoom;
    39.         }
    40.  
    41.         if (Transformm.GetComponent<Animation>().Play("Ttransform"))
    42.  
    43.         {
    44.             _state = State.Anim;
    45.  
    46.         }
    47.  
    48.         if (Transformm.GetComponent<Animation>().IsPlaying("Ttransform"))
    49.  
    50.         {
    51.             _state = State.CamSwitch;
    52.  
    53.         }
    54.  
    55.            
    56.         case State.Zoom: switch (_state)
    57.         {
    58.  
    59.             case State.Anim:
    60.                 Anim();
    61.                 break;
    62.  
    63.                 CamSwitch();
    64.                 break;
    65.             case State.CamSwitch:
    66.                 CamSwitch();
    67.                 break;
    68.  
    69.  
    70.  
    71.         }
    72.        
    73.     }
    74.  
    75.     public void Zoom()
    76.     {
    77.         Camera.transform.position = Vector3.MoveTowards(transform.position, Transformm.transform.position, moveSpeed * Time.deltaTime);
    78.         FPSController.active = false;
    79.  
    80.     }
    81.     public void Anim()
    82.     {
    83.         Transformm.GetComponent<Animation>().Play("Ttransform");
    84.  
    85.     }
    86.  
    87.     public void CamSwitch()
    88.     {
    89.  
    90.         Camera.active = false;
    91.         FPSController.active = true;
    92.  
    93.     }
    94. }
    95.  
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    You've stated what you are trying to achieve, so whats the problem you are facing with this script.
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the feedback. The following part isn't working. I'm not sure where to put it in the code to make it work.

    Code (CSharp):
    1.    
    2.         case State.Zoom: switch (_state)
    3.         {
    4.             case State.Anim:
    5.                 Anim();
    6.                 break;
    7.                 CamSwitch();
    8.                 break;
    9.             case State.CamSwitch:
    10.                 CamSwitch();
    11.                 break;
    12.         }
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I'm not a hardcore programmer, so I don't know about alternate methods of writing a switch-case statement if there are any, although the usual structure of a switch-case is:

    Code (CSharp):
    1. switch(condition-variable)
    2. {
    3.     case condition 1:
    4.         //*things to do under condition 1*
    5.         break;//to stop it from running the other conditions
    6.     case condition 2:
    7.         //*things to do under condition 2*
    8.         break;// to stop it from running the other conditions
    9.     case condition 3:
    10.     .
    11.     .
    12.     .//for however many intended conditions there are
    13.     default://for when the switch-case encounters an unaccounted condition
    14.         //*usually left empty*
    15.         break;
    16. }
    The rest will be changing the condition-variable when you need it and running the corresponding code under the current condition as you've already done with the if/else statements prior to what looks like a botched switch-case statement.
     
    Last edited: Feb 7, 2016
  5. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    did it work though.
     
  7. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    I haven't got it working yet. This is the new version of the code. The Cubey has a Transformm parented to it, and there's an animation of the Cubey.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Level1Test : MonoBehaviour
    5. {
    6.     public GameObject Camera;
    7.     public GameObject Transformm;
    8.     public GameObject FPSController;
    9.     public float moveSpeed;
    10.     public GameObject Cubey;
    11.  
    12.  
    13.  
    14.     public enum State
    15.     {
    16.         Zoom,
    17.         Anim,
    18.         CamSwitch
    19.     }
    20.  
    21.     public State _state;
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.     {
    26.      
    27.         _state = State.Zoom;
    28.  
    29.         switch (_state)
    30.         {
    31.  
    32.  
    33.             case State.Zoom:
    34.                 Zoomm();
    35.                 break;
    36.  
    37.             case State.Anim:
    38.                 Animm();
    39.                 break;
    40.  
    41.  
    42.             case State.CamSwitch:
    43.                 CamSwitchh();
    44.                 break;
    45.  
    46.  
    47.  
    48.         }
    49.     }
    50.  
    51.  
    52.  
    53.  
    54.     public void Zoomm()
    55.     {
    56.              Camera.active = true;
    57.              FPSController.active = false;
    58.             Camera.transform.position = Vector3.MoveTowards(transform.position, Transformm.transform.position, moveSpeed * Time.deltaTime);
    59.          
    60.          
    61.     }
    62.     public void Animm()
    63.     {
    64.        
    65.             Cubey.GetComponent<Animation>().Play("Ttransform");
    66.        
    67.     }
    68.  
    69.     public void CamSwitchh()
    70.     {
    71.         if (Transformm.GetComponent<Animation>().IsPlaying("Ttransform"))
    72.         {
    73.             Camera.active = false;
    74.             FPSController.active = true;
    75.             _state = State.CamSwitch;
    76.         }
    77.     }
    78.  
    79. }
    80.  
     
  8. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Right so to recap, you want the camera to move to a designated spot, where an animation will play. After the animation concludes, you want the player to regain control of the camera.

    So it seems you lack quite a lot of basic knowledge. I am willing to give you some explanations, although whether you are willing to read it is your own problem. I believe your understanding is more important than merely giving you a solution.



    Video Games
    When you play a Video game, you are actually watching a video you have vague control over. The game needs to change what is seen on screen based on the orders issued by the player(s). What you see on screen for that fraction of a second is called a frame.

    Like any Video games have a frame rate. The faster this frame rate it the more responsive the game feels, which is why there is a demand for 60fps and other fast speeds of frame refresh rate.

    Scripting
    A script, regardless of JavaScript(JS) or Csharp(C#), is the way you communicate, in this case to Unity, what you want to have happen. Unity does EXACTLY what you communicate it to do. If it does not work the way you want, it means your instructions did not communicate your intent.

    JS and C# in Unity is the language you use to write what you want to communicate. Like any other written language, it has rules like Grammar and Punctuation. If you violate these rules, Unity will not understand what you have written and tells you this through an error message. If Unity detects these mistakes a little later while running the game, that's when you have a crash since Unity just stops at something it does not understand.

    In case you don't know, you have been writing in C#.

    Scripting in Unity
    Unity has setup a framework through Monodevelop so your scripts have a structure to adhere to to make it function in Unity. Think of it as a 3-act-structure in writing or a letter format.

    The only parts of the format most beginners need to concern themselves with are only 2 functions, Start, and Update
    - Start runs only once, at the start of the level.
    - Update runs once every frame.



    So with that relatively brief explanation of things, lets relate it back to your provided script.

    At the Start,
    Code (CSharp):
    1.    void Start()
    2.     {
    3.         _state = State.Zoom;
    4.         switch (_state)
    5.         {
    6.             case State.Zoom:
    7.                 Zoomm();
    8.                 break;
    9.             case State.Anim:
    10.                 Animm();
    11.                 break;
    12.             case State.CamSwitch:
    13.                 CamSwitchh();
    14.                 break;
    15.         }
    16.     }
    your first line sets the _state to Zoom. You then ask for the script to do the behaviour stated in the Zoomm function, which I assume is something of a cinematic.

    in Zoom,
    Code (CSharp):
    1.     public void Zoomm()
    2.     {
    3.              Camera.active = true;
    4.              FPSController.active = false;
    5.             Camera.transform.position = Vector3.MoveTowards(transform.position, Transformm.transform.position, moveSpeed * Time.deltaTime);
    6.     }
    you command the script to activate the camera, and deactivate the player. You then command the Camera to move 1 step in the journey defined in MoveTowards.

    The rest of the script is not being run at all.
     
    Last edited: Feb 9, 2016
    cristo likes this.
  9. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the feedback, and taking the time.

     
  10. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Wouldn't it be easier to have it switch between two cameras? or get your player cam to movetowards a empty object (pivot) or even like this guy is doing in this video...

     
    cristo likes this.
  11. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks.