Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change camera on entry collider.

Discussion in 'Cinemachine' started by Wolf-nimations, Jul 16, 2019.

  1. Wolf-nimations

    Wolf-nimations

    Joined:
    Jul 16, 2019
    Posts:
    1
    I'm working on an RPG game and what I want to accomplish is that when the player enters a collider the camera changes to a preset different camera. Because normally the camera is focused on the player its self.

    For example in an area that has a nice view the camera switches to a different camera that has a better perspective of the player and the view.

    So when you enter the collider it switches to that special camera and when you leave the collider it switches back to the normal camera.

    Is there any way to accomplish this with cinemachine currently, or is there any script that can help with that?
    Like a way to tie the activation of a camera to the trigger of a collider. With some script i can add to a collider which specifies which camera it needs to activate on collision.

    -Wolf
     
  2. sevensixtytwo

    sevensixtytwo

    Joined:
    Apr 5, 2013
    Posts:
    27
    Hello,

    You can have multiple Virtual Cameras in the game world at the same time. Your camera will use the first VCam with the highest priority.

    Basically, this setup just switches between camera views. How the VCams are set up is still up to you:
    1. Gameplay VCam, priority 10
    2. Special VCam, priority 9
    3. Camera with CMBrain
    Then when entering the collider, you can change to Special VCam's priority to 11. This will cause your camera to transition towards the Special VCam's settings.

    Set Special's priority back to 9 when you exit the collider to return to the Gameplay VCam

    Hope this helps!
     
    Wolf-nimations likes this.
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    Yes, what @sevensixtytwo says is correct.

    Cinemachine comes with a helper script to facilitate this. Create your trigger collider, then add the CinemachineTriggerAction component to it. You can set it up to boost the vcam's priority when the player enters the trigger, and un-boost it when the player leaves. Or activate/deactivate the vcam. All without coding.

    upload_2019-7-17_8-38-53.png
     
    ThatMellon and Wolf-nimations like this.
  4. LForward1

    LForward1

    Joined:
    Jul 22, 2019
    Posts:
    12
    Ughh I've got a headache by looking to solve this problem.
     
    JMBhurst likes this.
  5. ThatMellon

    ThatMellon

    Joined:
    Sep 23, 2021
    Posts:
    8
    Heres a way to do it using a C# script although it does have its limitations.

    Create a script, copy and paste this C# text into it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CamColWorld : MonoBehaviour
    7. {
    8.     [SerializeField] CinemachineVirtualCamera InitialCam;
    9.     [SerializeField] CinemachineVirtualCamera ChangeCam;
    10.  
    11.     public void OnTriggerEnter(Collider other)
    12.     {
    13.         if (other.gameObject.tag == "Player")
    14.         {
    15.             ChangeCam.Priority = 1;
    16.             InitialCam.Priority = 0;
    17.         }
    18.     }
    19.     public void OnTriggerExit(Collider other)
    20.     {
    21.         if (other.gameObject.tag == "Player")
    22.         {
    23.             ChangeCam.Priority = 0;
    24.             InitialCam.Priority = 1;
    25.         }
    26.     }
    27. }
    Make sure your player object has the tag "Player" and attach this script to your collision object and make sure your collision object is a trigger, tag the box that says trigger.
    Remember to change the priorities to numbers that best suit your project.
    Last but not least; drag your virtual cameras into the ChangeCam and InitialCam box's in the inspector.
    The ChangeCam is the camera you want to change when inside the collision box and the InitialCam is the camera you want to change back to when you leave the collision area.

    The "OnTriggerEnter" method runs code when your player is inside the collision box and the 'OnTriggerExit" runs the code when you leave.

    The advice from @sevensixtytwo and @Gregoryl is also fairly solid!

    Hope this helps!
     
  6. aashikav

    aashikav

    Joined:
    Oct 7, 2021
    Posts:
    1
    Hey,
    I'm very new to the coding part of Unity. I'm hoping someone could help me out here. After looking through all the solutions, the one that worked for me was the script given by @ThatMellon .
    I have 4 Cinemachine Virtual cameras. Instead of going to the "ChangeCam" that I've picked, it goes to another Virtual camera. Should I be changing the script?
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    I suggest that you use the CinemachineTriggerAction behaviour instead of a custom script. No need for coding.