-
-
Save anandprabhakar0507/151f8e3743c379a56a11eee1352c475d to your computer and use it in GitHub Desktop.
A simple implementation of an infinite zooming technique a la Zoom Quilt (http://zoomquilt.org) for Unity 3d. Takes the player's position on the z axis and adjusts the cards to match. The cards should be rendered by an orthographic camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class ZoomCardController : MonoBehaviour { | |
/** Collection of zooming cards to be displayed. */ | |
public GameObject[] Cards; | |
/** Distance player travels between cards. */ | |
public float DistanceBetweenCards = 10; | |
void LateUpdate () { | |
// Locate player in world space. | |
Vector3 p = PlayerController.Instance.transform.position; | |
// Determine current travel fraction along the z-axis. | |
// Varies from [0..1] as the player moves between successive cards. | |
float t = (p.z % DistanceBetweenCards) / DistanceBetweenCards; | |
if (t < 0) | |
t += 1; | |
// Bias the fraction to smooth out view jerkiness. | |
// Not exactly sure why this helps, but it does.. :) | |
t = Mathf.Pow(t, 1.3f); | |
// Set up each card for current travel fraction. | |
int n = Cards.Length; | |
for (int i = 0; i<n; i++) | |
UpdateCard(Cards[i], i, n, t); | |
// Adjust alpha of the smallest card. | |
// The smallest card fades in as you get closer to it. | |
Cards[0].renderer.material.color = new Color(1, 1, 1, t); | |
} | |
private void UpdateCard(GameObject card, int i, int n, float t) | |
{ | |
float divisor = Mathf.Pow(3, n - 1); | |
float minZoom = Mathf.Pow(3, i) / divisor; | |
float maxZoom = Mathf.Pow(3, i + 1) / divisor; | |
float s = Mathf.Lerp(minZoom, maxZoom, t); | |
card.transform.localScale = new Vector3(s, s, s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment