playMaker

Author Topic: SetCameraPosition - For multiple cameras, Parallax scrolling, 2D  (Read 5080 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
SetCameraPosition - For multiple cameras, Parallax scrolling, 2D
« on: September 27, 2012, 02:57:26 PM »
This is for 2D games that want to have many layers of backgrounds that move at different speeds. To use this, you must have a camera for each background. The cameras move, not the scene. A lot less moving parts.

Here is how you would use this with a foreground and background:

1. In Hierarchy, create this folder structure:

Cameras (empty game object)
--Background (empty game object)
-----Camera (contains your ortho camera for background)
--Foreground (empty game object)
-----Camera (contains your ortho camera for foreground)

2. Camera Depth: make sure that each camera is using a different depth.

3. Culling Mask: make sure that each camera only includes the layers it should draw on screen.

4. Clear Flags: make sure that the foreground camera has this set to Depth Only. If a solid color is used, you won't see the background.

5. Use the SetCameraPosition action like this:
  • Game Object: Set this to the camera's container. For example, Background or Foreground. (don't attach to camera inside)
  • X, Y, Z: Set this to the coordinates of your primary camera container. (probably your foreground)
  • Speed X, Speed Y, Speed Z: Enter how fast you want the camera to move, related to how fast the primary camera is moving. 0 = no movement, <1 = slower speed, 1 = same speed, >1 = faster speed,  

Note: I use FingerGesturesDragCamera to move my primary camera (Foreground). I only use the above action for my secondary cameras.  http://hutonggames.com/playmakerforum/index.php?topic=1553.0

SetCameraPosition.cs code:
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Sets the Position of a Game Object. To leave any axis unchanged, set variable to 'None'.")]
public class SetCameraPosition : FsmStateAction
{
[RequiredField]
[Tooltip("The parent game object that contains the child camera game object.")]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.Variable)]
[Tooltip("Set individual axis below.")]
public FsmFloat x;
public FsmFloat y;
public FsmFloat z;

[Tooltip("How fast do you want the camera to move?")]
public FsmFloat speedX;
public FsmFloat speedY;
public FsmFloat speedZ;

[Tooltip("Use local or world space.")]
public Space space;

[Tooltip("Repeat every frame.")]
public bool everyFrame;

[Tooltip("Perform in LateUpdate. This is useful if you want to override the position of objects that are animated or otherwise positioned in Update.")]
public bool lateUpdate;

public override void Reset()
{
gameObject = null;

// default axis to variable dropdown with None selected.
x = new FsmFloat { UseVariable = true };
y = new FsmFloat { UseVariable = true };
z = new FsmFloat { UseVariable = true };

speedX = null;
speedY = null;
speedZ = null;

space = Space.Self;
everyFrame = false;
lateUpdate = false;
}

public override void OnEnter()
{
if (!everyFrame && !lateUpdate)
{
DoSetPosition();
Finish();
}
}

public override void OnUpdate()
{
if (!lateUpdate)
{
DoSetPosition();
}
}

public override void OnLateUpdate()
{
if (lateUpdate)
{
DoSetPosition();
}

if (!everyFrame)
{
Finish();
}
}

void DoSetPosition()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

Vector3 position;
position = space == Space.World ? go.transform.position : go.transform.localPosition;

// override any axis

if (!x.IsNone) position.x = x.Value * speedX.Value;
if (!y.IsNone) position.y = y.Value * speedY.Value;
if (!z.IsNone) position.z = z.Value * speedZ.Value;

// apply

if (space == Space.World)
{
go.transform.position = position;
}
else
{
go.transform.localPosition = position;
}
}


}
}
« Last Edit: September 27, 2012, 03:00:47 PM by amaranth »