Skip to content

Instantly share code, notes, and snippets.

@vnavarro
Created May 5, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vnavarro/0526a8dfeab788ba06108830184c3193 to your computer and use it in GitHub Desktop.
Save vnavarro/0526a8dfeab788ba06108830184c3193 to your computer and use it in GitHub Desktop.
A camera switcher c# script for Unity3D created using version 2017, but should work with almost any of them.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// A camera switcher. It cicles camera transform through given positions starting from a specified position walking 1 by 1.
/// It can either change or animate between positions with the specified speed.
/// </summary>
public class VNCameraSwitch : MonoBehaviour {
// Store camera possible positions, including first position
public Transform[] positions;
// Current camera position, also used to set initial camera position
public int currentPosition = 0;
// The key that is going to change the camera
public KeyCode switchKey = KeyCode.Alpha2;
// The speed for animating between positions
public float speed = 20f;
// Choose between animate or not when changing positions
public bool useAnimation = true;
/// <summary>
/// Sets the camera initial position to the specified currentPosition.
/// </summary>
private void SetCameraInitialPosition() {
if (positions.Length > 0) {
UpdateCameraPosition ();
}
}
/// <summary>
/// Check if the given position is the same as the camera transform.
/// </summary>
/// <returns><c>true</c>, if equals to camera position, <c>false</c> otherwise.</returns>
/// <param name="positionToVerify">The transform with position and rotation to verify.</param>
bool isEqualsToCameraPosition(Transform positionToVerify) {
return transform.position == positionToVerify.position &&
transform.rotation == positionToVerify.rotation;
}
/// <summary>
/// Updates the camera transform to positions[currentPosition]
/// </summary>
void UpdateCameraPosition() {
transform.rotation = positions[currentPosition].rotation;
transform.position = positions[currentPosition].position;
}
/// <summary>
/// Switchs the camera position when the switchKey is pressed and camera is right at currentPosition.
/// That means the camera is not allowed to change during animation.
/// If useAnimation is true only the currentPosition variable is updated, changing camera's virtual position
/// to a desired position to be animated to.
/// </summary>
void switchCameraPositionIfNeeded() {
if (Input.GetKeyUp (switchKey) && isEqualsToCameraPosition (positions [currentPosition])) {
currentPosition++;
currentPosition = currentPosition % positions.Length;
if (!useAnimation) {
UpdateCameraPosition ();
}
}
}
/// <summary>
/// Change camera transform towards desired position (currentPosition in positions)
/// </summary>
void IncrementPosition ()
{
float delta = speed * Time.deltaTime;
Transform endPosition = positions [currentPosition];
Vector3 nextPosition = Vector3.MoveTowards (transform.position, endPosition.position, delta);
Quaternion nextRotation = Quaternion.RotateTowards(transform.rotation, endPosition.rotation, delta);
transform.position = nextPosition;
transform.rotation = nextRotation;
}
/// <summary>
/// Animate camera towards new position with animation is on and is not between animations
/// </summary>
void AnimateIfNeeded() {
bool shouldAnimate = useAnimation && !isEqualsToCameraPosition (positions [currentPosition]);
if (shouldAnimate) {
IncrementPosition ();
}
}
void Start()
{
SetCameraInitialPosition ();
}
void Update()
{
switchCameraPositionIfNeeded ();
AnimateIfNeeded ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment