You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.7 KiB
86 lines
2.7 KiB
11 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
|
/**
|
||
|
* Attach this component to a sprite object to apply a simple parallax scrolling effect.
|
||
11 years ago
|
* The horizontal and vertical parallax offset is calculated based on the distance from the camera to the position of the parent Room.
|
||
|
* The scale parallax is calculated based on the ratio of the camera size to the size of the Room. This gives a 'dolly zoom' effect.
|
||
11 years ago
|
* Accelerometer based parallax may also be applied on devices that support it.
|
||
11 years ago
|
*/
|
||
|
public class Parallax : MonoBehaviour
|
||
|
{
|
||
|
/**
|
||
|
* Scale factor for calculating the parallax offset.
|
||
|
*/
|
||
11 years ago
|
public Vector2 parallaxScale = new Vector2(0.25f, 0f);
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Scale factor when camera is zoomed out to show the full Room.
|
||
|
* This will typically be set to 1 to show the sprite at normal size.
|
||
|
*/
|
||
|
public float zoomedOutScale = 1f;
|
||
|
|
||
|
/**
|
||
|
* Scale factor when camera is fully zoomed in on Room.
|
||
|
* Setting this to a value greater than 1 will give a 'dolly zoom' effect when zooming in.
|
||
|
*/
|
||
|
public float zoomedInScale = 1f;
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Scale factor for calculating parallax offset based on device accelerometer tilt angle.
|
||
|
* Set this to 0 to disable the accelerometer parallax effect.
|
||
|
*/
|
||
|
public float accelerometerScale = 0.5f;
|
||
|
|
||
11 years ago
|
Vector3 startPosition;
|
||
10 years ago
|
// Vector3 startScale;
|
||
11 years ago
|
|
||
11 years ago
|
Vector3 acceleration;
|
||
|
Vector3 velocity;
|
||
|
|
||
11 years ago
|
void Start ()
|
||
|
{
|
||
11 years ago
|
// Store the starting position and scale of the sprite object
|
||
11 years ago
|
startPosition = transform.position;
|
||
10 years ago
|
// startScale = transform.localScale;
|
||
11 years ago
|
}
|
||
|
|
||
|
void Update ()
|
||
|
{
|
||
11 years ago
|
Vector3 translation = Vector3.zero;
|
||
|
|
||
|
// Apply parallax translation based on camera position relative to Room
|
||
|
{
|
||
10 years ago
|
Vector3 a = startPosition;
|
||
11 years ago
|
Vector3 b = Camera.main.transform.position;
|
||
|
translation = (a - b);
|
||
|
translation.x *= parallaxScale.x;
|
||
|
translation.y *= parallaxScale.y;
|
||
|
}
|
||
|
|
||
|
// Apply parallax translation based on device accelerometer
|
||
|
if (SystemInfo.supportsAccelerometer)
|
||
|
{
|
||
|
float maxParallaxScale = Mathf.Max(parallaxScale.x, parallaxScale.y);
|
||
|
// The accelerometer data is quite noisy, so we apply smoothing to even it out.
|
||
|
acceleration = Vector3.SmoothDamp(acceleration, Input.acceleration, ref velocity, 0.1f);
|
||
|
// Assuming a 45 degree "neutral position" when holding a mobile device
|
||
|
Vector3 accelerometerOffset = Quaternion.Euler(45, 0, 0) * acceleration * maxParallaxScale * accelerometerScale;
|
||
|
translation += accelerometerOffset;
|
||
|
}
|
||
|
|
||
|
transform.position = startPosition + translation;
|
||
11 years ago
|
|
||
|
// Set new scale for sprite
|
||
10 years ago
|
/*
|
||
11 years ago
|
float roomSize = parentRoom.renderer.bounds.extents.y;
|
||
|
float t = Camera.main.orthographicSize / roomSize ;
|
||
|
float scale = Mathf.Lerp (zoomedInScale, zoomedOutScale, t);
|
||
|
transform.localScale = startScale * scale;
|
||
10 years ago
|
*/
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|