|
|
|
@ -5,7 +5,8 @@ namespace Fungus
|
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* Attach this component to a sprite object to apply a simple parallax scrolling effect. |
|
|
|
|
* The parallax offset is calculated based on the distance from the camera to the position of the parent Room. |
|
|
|
|
* 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. |
|
|
|
|
*/ |
|
|
|
|
public class Parallax : MonoBehaviour |
|
|
|
|
{ |
|
|
|
@ -14,14 +15,28 @@ namespace Fungus
|
|
|
|
|
*/ |
|
|
|
|
public Vector2 parallaxScale = new Vector2(0.25f, 0f); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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; |
|
|
|
|
|
|
|
|
|
Vector3 startPosition; |
|
|
|
|
Vector3 startScale; |
|
|
|
|
|
|
|
|
|
Room parentRoom; |
|
|
|
|
|
|
|
|
|
void Start () |
|
|
|
|
{ |
|
|
|
|
// Store the starting position of the sprite object |
|
|
|
|
// Store the starting position and scale of the sprite object |
|
|
|
|
startPosition = transform.position; |
|
|
|
|
startScale = transform.localScale; |
|
|
|
|
|
|
|
|
|
// Store a reference to the parent Room object |
|
|
|
|
Transform ancestor = transform.parent; |
|
|
|
@ -51,10 +66,19 @@ namespace Fungus
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Vector3 offset = Game.GetInstance().GetParallaxOffset(parallaxScale); |
|
|
|
|
|
|
|
|
|
// Set new position for sprite |
|
|
|
|
Vector3 a = parentRoom.transform.position; |
|
|
|
|
Vector3 b = Camera.main.transform.position; |
|
|
|
|
Vector3 offset = (a - b); |
|
|
|
|
offset.x *= parallaxScale.x; |
|
|
|
|
offset.y *= parallaxScale.y; |
|
|
|
|
transform.position = startPosition + offset; |
|
|
|
|
|
|
|
|
|
// Set new scale for sprite |
|
|
|
|
float roomSize = parentRoom.renderer.bounds.extents.y; |
|
|
|
|
float t = Camera.main.orthographicSize / roomSize ; |
|
|
|
|
float scale = Mathf.Lerp (zoomedInScale, zoomedOutScale, t); |
|
|
|
|
transform.localScale = startScale * scale; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|