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.
85 lines
3.5 KiB
85 lines
3.5 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
11 years ago
|
using UnityEngine;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Attach this component to a sprite object to apply a simple parallax scrolling effect.
|
||
|
/// The horizontal and vertical parallax offset is calculated based on the distance from the camera to the position of the background sprite.
|
||
|
/// The scale parallax is calculated based on the ratio of the camera size to the size of the background sprite. This gives a 'dolly zoom' effect.
|
||
|
/// Accelerometer based parallax is also applied on devices that support it.
|
||
|
/// </summary>
|
||
8 years ago
|
public class Parallax : MonoBehaviour
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// The background sprite which this sprite is layered on top of.
|
||
|
/// The position of this sprite is used to calculate the parallax offset.
|
||
|
/// </summary>
|
||
8 years ago
|
[SerializeField] protected SpriteRenderer backgroundSprite;
|
||
11 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Scale factor for calculating the parallax offset.
|
||
|
/// </summary>
|
||
8 years ago
|
[SerializeField] protected Vector2 parallaxScale = new Vector2(0.25f, 0f);
|
||
11 years ago
|
|
||
8 years ago
|
/// <summary>
|
||
|
/// Scale factor for calculating parallax offset based on device accelerometer tilt angle.
|
||
|
/// Set this to 0 to disable the accelerometer parallax effect.
|
||
|
/// </summary>
|
||
8 years ago
|
[SerializeField] protected float accelerometerScale = 0.5f;
|
||
11 years ago
|
|
||
8 years ago
|
protected Vector3 startPosition;
|
||
|
protected Vector3 acceleration;
|
||
|
protected Vector3 velocity;
|
||
11 years ago
|
|
||
8 years ago
|
protected virtual void Start ()
|
||
|
{
|
||
|
// Store the starting position and scale of the sprite object
|
||
|
startPosition = transform.position;
|
||
10 years ago
|
|
||
8 years ago
|
// Default to using parent sprite as background
|
||
|
if (backgroundSprite == null)
|
||
|
{
|
||
|
backgroundSprite = gameObject.GetComponentInParent<SpriteRenderer>();
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
protected virtual void Update ()
|
||
|
{
|
||
|
if (backgroundSprite == null)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
Vector3 translation = Vector3.zero;
|
||
11 years ago
|
|
||
8 years ago
|
// Apply parallax translation based on camera position relative to background sprite
|
||
|
{
|
||
|
Vector3 a = backgroundSprite.bounds.center;
|
||
|
Vector3 b = Camera.main.transform.position;
|
||
|
translation = (a - b);
|
||
|
translation.x *= parallaxScale.x;
|
||
|
translation.y *= parallaxScale.y;
|
||
|
translation.z = 0;
|
||
10 years ago
|
|
||
8 years ago
|
// TODO: Limit parallax offset to just outside the bounds of the background sprite
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
// 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;
|
||
|
}
|
||
11 years ago
|
|
||
8 years ago
|
transform.position = startPosition + translation;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|