Browse Source

Added parallax scaling effect when zooming in.

master
chrisgregan 11 years ago
parent
commit
bc7c7e7e19
  1. 8
      Assets/Fungus/Scripts/CameraController.cs
  2. 23
      Assets/Fungus/Scripts/Game.cs
  3. 32
      Assets/Fungus/Scripts/Parallax.cs

8
Assets/Fungus/Scripts/CameraController.cs

@ -351,14 +351,6 @@ namespace Fungus
swipePanViewB = null;
}
/**
* Returns the current position of the main camera.
*/
public Vector3 GetCameraPosition()
{
return Camera.main.transform.position;
}
void SetCameraZ()
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x, Camera.main.transform.position.y, cameraZ);

23
Assets/Fungus/Scripts/Game.cs

@ -194,29 +194,6 @@ namespace Fungus
return 0;
}
/**
* Returns a parallax offset vector based on the camera position relative to the active Room.
* Higher values for the parallaxFactor yield a larger offset (appears closer to camera).
* Suggested range for good parallax effect is [0.1..0.5].
* @param parallaxFactor Horizontal and vertical scale factors to apply to camera offset vector.
* @return A parallax offset vector based on camera positon relative to current room and the parallaxFactor.
*/
public Vector3 GetParallaxOffset(Vector2 parallaxFactor)
{
if (activeRoom == null)
{
return Vector3.zero;
}
Vector3 a = activeRoom.transform.position;
Vector3 b = cameraController.GetCameraPosition();
Vector3 offset = (a - b);
offset.x *= parallaxFactor.x;
offset.y *= parallaxFactor.y;
return offset;
}
/**
* Loads a new scene and displays an optional loading image.
* This is useful for splitting a large Fungus game across multiple scene files to reduce peak memory usage.

32
Assets/Fungus/Scripts/Parallax.cs

@ -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;
}
}
}

Loading…
Cancel
Save