Search Unity

Smooth camera transition with trigger.

Discussion in 'Getting Started' started by sws90., Oct 2, 2015.

  1. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    Hello. I need help setting up a smooth camera transition with a trigger, so when the player enters a trigger, the camera smoothly transitions to the new one, from the default. Then vice versa when the player exits the trigger. I would also like to control the transition time.
    For the default camera, I'm using the smooth look at script, and the smooth follow script.
    I'm using unity 5, and in c#.
    Thanks :)
     
  2. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Smoothly moving the camera to a new position is easily done using a tweening engine ( eg. DOTween). Actually getting the position to move to is slightly trickier: you'll want to write a script that responds to OnTriggerEnter() and set the object's collider to "Trigger". Then place empty Game Objects within your scene to link to the script.
     
  3. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    Could you provide a script to do this? I'm not really sure how to go about coding it.
     
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    I could write a script for this tonight, but in the meantime this task is basically the first thing in Chapter 8 of my book. I actually go over how to open a door when triggered, not move a camera, but positioning an object doesn't matter what kind of object you're moving.
     
  5. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    Sorry, but I'm not going to pay for something that has only one thing in it that I need.
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Nor would I! Although if you're asking about this then I suspect there's other stuff that you'd want to know too, but whatever that's up to you (you can browse the table of contents).

    Anyway, here's some code that moves to a given position when triggered:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraTrigger : MonoBehaviour {
    5.     [SerializeField] private Camera camera;
    6.     [SerializeField] private Transform target1;
    7.     [SerializeField] private Transform target2;
    8.    
    9.     void OnTriggerEnter(Collider other) {
    10.         camera.gameObject.transform.position = target1.position;
    11.         camera.gameObject.transform.rotation = target1.rotation;
    12.     }
    13.  
    14.     void OnTriggerExit(Collider other) {
    15.         camera.gameObject.transform.position = target2.position;
    16.         camera.gameObject.transform.rotation = target2.rotation;
    17.     }
    18. }
     
  7. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    Thanks for the code, but it does not seem to be working. Or maybe I'm using it wrong?
    I made a new camera then moved it to the position I want it to switch to when the player enters the trigger, but nothing is happening when the player enters the trigger.
    I attached the script to the camera trigger, set the Camera to the new camera, Target 1 to Player, and Target 2 also to Player.
     
  8. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    It's just one camera that moves around, not multiple cameras. Put this script on the trigger object, link the camera to Camera, and link target positions (ie. empty game objects) to the Target variables.
     
  9. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    Ok, that works. But, the transition is not smooth and when the player exits the trigger, it does not revert back to the normal camera.
     
  10. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Well yeah, making the movement smooth requires DOTween I mentioned before. To adjust the code for that, download the code from their website and follow the examples.

    As for reverting back, it goes to whatever you set for 'target2'. In the code you can see that OnTriggerEnter goes to 'target1' while OnTriggerExit goes to 'target2'; "enter" is when you enter the trigger area, and "exit" is when you leave the trigger area.
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Customer: Does anyone have any milk? I'm thirsty and I simply love chocolate milk.
    Farmer: I have this great cow that makes tasty milk. If you buy it, you'll have all the tasty milk you want!
    Customer: Uh, no. I just want a single glass. Can't you just give it to me?
    Farmer: Sure, I guess. Here you go.
    Customer: Thanks, but this isn't chocolate. Don't you have any syrup you could put in it? I have some here, but I don't want to dirty a spoon.
     
    Belacalac, ROTHAN, bturep and 5 others like this.
  12. sws90.

    sws90.

    Joined:
    Mar 7, 2015
    Posts:
    11
    I just found out about lerp, can't I just use something like that to smoothly move the camera? (Though, I'm not sure how to go about that yet. lol) I'd rather use what unity has by default.
     
  13. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    You can use lerp to do smooth linear movements, so I do recommend looking that up in the Script Reference and trying it out so that you understand how.

    But ultimately I highly recommend a tweening engine because it's useful for a lot more than linear movements (eg. you can ease in and out of the movement). I tend to be the same way actually, preferring what's built-in when I can, but in this case the library is so useful that it's more than worth it. It's totally free after all, and just by changing one line of code you can try out many different subtle variations on the movement in order to polish the look.