Skip to content

Instantly share code, notes, and snippets.

@caseypugh
Last active August 1, 2018 15:22
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 caseypugh/4319d687cde40193acd101a8903bbd9a to your computer and use it in GitHub Desktop.
Save caseypugh/4319d687cde40193acd101a8903bbd9a to your computer and use it in GitHub Desktop.
Simple script to animate a Unity and synchronize to video playback.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vimeo.Player;
public class TinyPlanetCam : MonoBehaviour {
[Range(1f, 100f)]
public float fovAmplitude = 1f;
[Range(1f, 100f)]
public float fovRange = 1f;
[Range(0f, 0.1f)]
public float yAxisAmplitude = 0.0001f;
[Range(0f, 20f)]
public float yAxisRange = 1f;
[Range(0f, 1f)]
public float rotationAmplitude = 0.001f;
[Range(0f, 20f)]
public float rotationRange = 1f;
public VimeoPlayer vimeoPlayer;
private float timePassed = 0;
void Awake()
{
// If a VimeoPlayer is assigned, it will sync animation to the frames of the video
if (vimeoPlayer != null) {
vimeoPlayer.OnVideoStart += VideoStart;
vimeoPlayer.OnFrameReady += VideoFrameUpdate;
}
}
void Update()
{
if (vimeoPlayer == null) {
timePassed = Time.time;
UpdateCamera();
}
}
private void VideoStart()
{
vimeoPlayer.controller.SendFrameReadyEvents();
}
private void VideoFrameUpdate()
{
// Update time passed based upon the frame rate of the video
timePassed += 1f / vimeoPlayer.controller.videoPlayer.frameRate;
UpdateCamera();
}
private void UpdateCamera()
{
GetComponent<Camera>().fieldOfView = 120f + Mathf.Sin(timePassed / fovRange) * fovAmplitude;
GetComponent<Camera>().transform.Translate(0, 0, Mathf.Cos(timePassed / yAxisRange) * yAxisAmplitude);
GetComponent<Camera>().transform.Rotate(0, 0, Mathf.Cos(timePassed / rotationRange) * rotationAmplitude);
}
private void OnDisable()
{
if (vimeoPlayer != null) {
vimeoPlayer.OnFrameReady -= VideoFrameUpdate;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment