Home > Software design >  Camera switching script for 8 directional cameras?
Camera switching script for 8 directional cameras?

Time:05-19

Im new to coding and am tying to figure out my a camera swapping script. I have 8 Cinemachine virtual cameras positioned around the player in 45 degree increments around the Y-axis for each direction(N,NE,E,SE,S,SW,W,NW). My plan is to use "Q" and "E" keys to transition through each camera to create a full rotation around the player but with the fixed perspective of the 8 cameras.

I have my 8 cameras set up in the State Drive and animator. I want "Q" to loop through the cameras to create a counterclockwise rotation around the player and "E" to be clockwise.

[List of Cameras Animator Cameras in Scene

CodePudding user response:

If you use Cinemachine, you are definitely familiar with the State Driven Camera. This feature can switch virtual cameras via an Animator. For example, after creating a state driven from the Cinemachine menu, I put three virtual cameras with the following names in it and adjusted each one to the desired angles.

enter image description here


Cinemachine State Driven Camera Setup

At the bottom, you will see the State Driver Camera component, to activate virtual cameras, create an animator and place it in Animated Target. This camera automatically detects animator states and moves the cameras according to changing states:

enter image description here

I set the animator as shown below and set the names of the states as well as the parameters to make it easier.

enter image description here enter image description here


Switching Camera Script

Finally, using the code below, refer the keys to change the parameter and you can see the change of cameras.

public Animator stateDrivenAnimator; // fill state driven animator here

public List<KeyCode> KeyCodes = new List<KeyCode>() // some keys for example
{
    KeyCode.Q,
    KeyCode.W,
    KeyCode.E
};
public void Update()
{
    KeyCodes.ForEach(k => { if (Input.GetKeyDown(k)) stateDrivenAnimator.SetTrigger(k.ToString()); });
}

Rotate clockwise and counterclockwise

For this purpose, it is better to define the camera rotation process in the Index. It is enough to define only one Index parameter instead of Triggers and condition every state by rotation cycle in order like the image.

  • (N == 0)
  • (NE == 1)
  • (E == 2)
  • (SE == 3)
  • (S == 4)
  • (SW == 5)
  • (W == 6)
  • (NW == 7)

enter image description here enter image description here

Finally, the bottom code rotates the Index in the loop with CW and CCW keys instead of placing the key letters in the Trigger, and the cameras are set according to the Index.

public CinemachineStateDrivenCamera stateDriven; // Get state-drive here

public KeyCode CW;
public KeyCode CCW;

private int index;

public void Update()
{
    if (!Input.GetKeyDown(CW) && !Input.GetKeyDown(CCW)) return;
    var animator = stateDriven.m_AnimatedTarget;
    var childCount = stateDriven.transform.childCount;
    
    if (Input.GetKeyDown(CW)) index =   index % childCount;
    else if (Input.GetKeyDown(CCW)) index = (index == 0) ? childCount - 1 : --index;
    
    animator.SetInteger("Index", index);
}
  • Related