Search Unity

Camera code for cut scene attempt. C#

Discussion in 'Scripting' started by cristo, Nov 27, 2014.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi,

    Please just let me explain what I'm trying to do with this code.

    I'm trying to have the Player be able to walk into a trigger, and when they do so, have the Player Camera be disabled, and then for a Second Camera to be enabled.
    The Player Camera's mesh renderer should become disabled so the second camera can't see it. Also the second camera's mesh renderer should stay disabled so the first person camera can't see it.
    When the Player enters the trigger, I'd like the Second Camera's animation to be played, which is a preanimated movement curve of the second camera, which is in the same location the FPC has walked into.
    Creating a cut scene.
    Then after the animation is played, I'd like the Second Camera to be disabled and the FPC camera to be true again, but only after the animation has played, restoring control to the Player.

    Unfortunately I don't really know what I'm doing and I'm new to coding. I don't know how far off my logic is. I don't know whether I should add a yield after the animation to ensure enough seconds pass for the animation to finish playing before control goes back to the first camera.

    Anyway, any insight from people who are much more experienced than me would be greatly appreciated.



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class CameraSwitch : MonoBehaviour {
    4.   private bool  Camera1 = true;
    5.   private bool  Camera2 = false;
    6.   public AnimationClip cameraLook;
    7.   void Start ()
    8.   {
    9.   Camera2 = GameObject.Find ( "Camera2" );
    10.   GameObject.Find("Graphics").GetComponent(MeshRenderer).enabled = false;
    11.   }
    12.   void OnTriggerEnter ( Collider other )
    13.   {
    14.   if (other.tag == "Player") {
    15.   GameObject.Find ("Camera2");
    16.   animation.Play ("CameraLook");
    17.   GameObject.Find ("Camera1").GetComponent (FPSInputController).enabled = false;
    18.   GameObject.Find ("Graphics").GetComponent (MeshRenderer).enabled = false;
    19.   GameObject.Find ("Camera2").enabled = true;
    20.   }
    21.   }
    22.   public void OnTriggerExit ( Collider other )
    23.   {
    24.   if (other.tag == "Player") {
    25.   GameObject.Find ("Camera1").GetComponent(FPSInputController).enabled = true;
    26.   GameObject.Find ("Graphics").GetComponent (MeshRenderer).enabled = true;
    27. GameObject.Find ("Camera2").enabled = false;
    28.   }
    29.   }
    30.   }
     
    Last edited: Nov 28, 2014
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
  3. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    I did this exact thing recently, you seem like you have the logic in order, it's just a matter of researching the proper code.


    Code (CSharp):
    1. public Camera cam1;
    2. public Camera cam2;
    3.  
    4. void OnTriggerEnter(Collider c)
    5. {
    6.     cam1.gameObject.SetActive(false);
    7.     cam2.gameObject.SetActive(true);
    8.    
    9.     // I'm unsure if you can just go
    10.     // cam2.gameObject.animation.Play(); or not... experiment!$!$
    11.     // Cam2Script is the name of a script attached to the cutscene camera
    12.     // functionThatRunsAnimation() is a public function in that script with
    13.     // the sole line
    14.     // animation.Play();  Assuming there's only one animation
    15.     cam2.gameObject.GetComponent<Cam2Script>().functionThatRunsAnimation();
    16. }
    17.  
    18. // Furthermore, animations can use events.  So, you can add an event at the
    19. // end of your animation to call a function such as this one.
    20.  
    21. public void ReturnCamerasToNormal()
    22. {
    23.     cam2.gameObject.SetActive(false);
    24.     cam1.gameObject.SetActive(true);
    25. }
    Disabling and enabling the gameobject should keep every piece of the disabled objects invisible.

    Good luck :> I had a button that called the script.. worked flawlessly.. client was impressed.. but got fired before I could finish it up ;)
     
  4. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Cheers, thanks, I didn't know about code tags. Thanks for the heads up.
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What kind of mesh is attached to your cameras so that it needs to be disabled?

    The general idea is okay, though.
    You could create public variables of type Camera and other stuff that you need to address and assign them in in the inspector, so you just don't need to find it everytime you need to switch.

    If something does not work, provide some detailed information.
     
  6. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    Cristo, I have a reply but it's sitting here "awaiting moderation" I'm wondering if it'll ever show up..
     
    cristo likes this.
  7. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi Suddoha and wkarma, thanks for your feedback. Alternately I was thinking of rewriting the code along the following lines: (Hopefully once my weekend hits, I will have three full days to work on this code... Time...)

    Suddoha,
    this idea to disable the mesh is so that the cameras aren't visible to each other when they are activated. I may have been wrong about that. I'm sorry, I've literally got to leave to go to work!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TrueCameraSwitch : MonoBehaviour {
    5.  
    6.     bool isSecondary;
    7.     public GameObject mainCamera;
    8.     public GameObject secondaryCamera;
    9.  
    10.     void Start () {
    11.  
    12.         isSecondary = false;
    13.         GameObject.Find("CamTrigger");
    14.         GameObject.Find("Player").GetComponent(FPSInputController).enabled = true;
    15.         GameObject.Find("Graphics").GetComponent(MeshRenderer).enabled = false;
    16.         GameObject.Find("Camera2").GetComponent(FPSInputController).enabled = true;
    17.         GameObject.Find("Graphics").GetComponent(MeshRenderer).enabled = false;
    18.  
    19.     }
    20.  
    21.  
    22.     void OnTriggerEnter ( Collider other )
    23.  
    24.         {
    25.                 if (other.tag == "Player") {
    26.                         GameObject.Find ("Camera2");
    27.                         animation.Play ("CameraLook");
    28.                         isSecondary = !isSecondary;
    29.                      
    30.                 }
    31.  
    32.                 if (isSecondary == false) {
    33.                         secondaryCamera.camera.active = false;
    34.                         mainCamera.camera.active = true;
    35.                 } else {
    36.                         secondaryCamera.camera.active = true;
    37.                         mainCamera.camera.active = false;
    38.                 }
    39.         }
    40.  
    41. }
    42.  
    43.  
    44.  
    45.  
     
  8. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    cristo, the most important parts of my post being held up by the bull**** mod system...

    public Camera cam1;
    public Camera cam2;

    this command will disable a camera object completely...
    cam1.gameObject.SetActive(false);

    Cameras are not visible to other cameras unless they have a renderer of some kind rendering something. However, you would still see the maincamera even if the other one is enabled, that's why you're right with disabling one while enabling the other.

    The other important piece of information is that you can set an event to run in an animation. This event will call a function on another object at whatever point in the animation the event is set.

    I set it to the end of my animation when I did this.

    Logic was this:

    disable the first camera
    enabled the second camera
    play the second camera's animation
    at the end of the animation an event is fired
    this event disabled the second camera and
    enables the first again

    I had it set to start when a button is clicked.
     
  9. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for your advice wkarma,. I will definitely apply it. I hope they approve of your code post. I'm going to try to work it out over the next couple of days and I will post what happens. Hopefully success and then everyone can share the technique if they want it. :)
     
  10. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hey wkarma and Suddoha, through mixing together some great youtube tutes by speedtutor and experimenting all day with your advice wkarma, I managed to get a code working, to do what i was trying to do.

    The only two things I don't like about it is that it's not in C# and the First Person Controller can still move during the animation.

    I tried going:

    Code (JavaScript):
    1. GameObject.Find("Player").GetComponent(FPSInputController).enabled = false;
    2. yield WaitForSeconds (5);
    3. GameObject.Find("Player").GetComponent(FPSInputController).enabled = true;
    after the animation plays, but, as yet, it isn't quite working to freeze the player until after the animation. Maybe that event technique wkarma mentioned.

    In any case, it's been a great day, as I didn't expect to have anything working at all today.


    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cam1 : Camera;
    4. var cam2 : Camera;
    5.  
    6. private var walkedIn : boolean = false;
    7.  
    8. function Start ()
    9. {
    10. cam1.camera.enabled = true;
    11. cam2.camera.enabled = false;
    12.  
    13. }
    14.  
    15. function OnTriggerEnter (Col : Collider)
    16.     {
    17.     if (Col.tag == "Player")
    18.     {
    19.     walkedIn = !walkedIn;
    20.     }
    21.     cam2.camera.enabled = true;
    22.     cam1.camera.enabled = false;
    23.     GameObject.Find("Player").GetComponent(FPSInputController).enabled = true;
    24.     GameObject.Find("Graphics").GetComponent(MeshRenderer).enabled = false;
    25.     cam2.animation.Play();
    26.      }
    27.  
    28.  
    29. function OnTriggerExit (Col : Collider)
    30.     {
    31.     if (Col.tag == "Player")
    32.     {
    33.     walkedIn = !walkedIn;
    34.     }
    35.     cam2.camera.enabled = false;
    36.     cam1.camera.enabled = true;
    37.     GameObject.Find("Player").GetComponent(FPSInputController).enabled = true;
    38.     GameObject.Find("Graphics").GetComponent(MeshRenderer).enabled = true;
    39.     cam2.animation.Stop();
    40.     }
    41.  
    42.  
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Well it keeps moving because the translatation, rotation etc is calculated in the CharacterMotor script.

    You should probably disable all the scripts which contribute to the input, probably also MouseLook. If you want to stop your character from falling, sliding etc you can also disable the CharacterController afterwards.
     
  12. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Would be simple to make that c# Cristo.

    Code (CSharp):
    1. public Camera cam1
    2. public Camera cam2
    3.  
    4. private bool walkedIn = false;
    The rest would be just saying void instead of function in front of the functions.

    At least I think haha might give you a start in the right direction anyway.

    Good Luck regardless.