- Home /
I do I get 2 virtual cameras within Cinemachine to interpolate between each other based on the distance of two game objects?
Hey all,
Ive set up two virtual cameras within cinemachine, in my code i've got them named as "Battle Camera1" and "Battle Camera2".
Basically, I want the either camera to gain higher priority based on the distance between the player and the enemy AI object.
In battle camera one the priority is higher if the player and the enemy AI character is farther apart, while this camera is active the player's view is of an over head shot of the world.
When the player and the enemy AI is close to each other and engaging in combat the Battle Camera 2 should be active, that camera view is more intimate, and close to showcase the visceral nature .
code is as follows.
Thanks for your insight or help.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using Cinemachine.Utility;
public class CameraZoom : MonoBehaviour
{
//Virtual Camera Variables that will store the Virtual camers that will swap based on distance
[SerializeField]
private GameObject[] battleCamera;
//Variable for storing the Camerablend component
// [SerializeField]
// private CameraBlender vCamBlender;
//Virtual Camera Priority/Weight Variables that store the priority of the dominant Camera
[SerializeField]
private float battleCamOnePriority;
[SerializeField]
private float battleCamTwoPriority;
//Distance Variable for storing distance
[SerializeField]
private float enemyDistance;
[SerializeField]
private float playerDistance;
//Actor game object Variables to store the game characters
[SerializeField]
private GameObject player;
[SerializeField]
private GameObject enemies;
//State of Virtual Cameras that will become active and inactive depending on the player's distances
private bool battleCameraOneIsActive = true;
private bool battleCameraTwoIsActive = false;
void Awake()
{
//upon starting the scene, Distance variables are set to the player and enemy's transoforms
playerDistance = Vector3.Distance(player.transform.position, enemies.transform.position);
enemyDistance = Vector3.Distance(enemies.transform.position, player.transform.position);
}
// activating the virtual cameras
public void ActivateCamera(int index)
{
for (int i = 0; i < battleCamera.Length; i++)
{
if (i == index && playerDistance > enemyDistance)
{
battleCamera[i].SetActive(true);
battleCamOnePriority = 5;
battleCamTwoPriority = 0;
}
else
{
battleCamera[i].SetActive(false);
battleCamTwoPriority = 0;
}
}
for (int i = 0; i < battleCamera.Length; i++)
{
if (i== index && playerDistance <= enemyDistance)
{
battleCamera[i].SetActive(true);
battleCamTwoPriority = 5;
battleCamOnePriority = 0;
}
else
{
battleCamera[i].SetActive(false);
battleCamTwoPriority = 0;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Restrict Movement on the Y-Axis When Using LookAt.Transform? 1 Answer