Hi everyone, I've created a script that moves around the camera with the mouse or finger, using FingerGestures. This script was customized from the FingerGesturesDragObject script.
Original code below. For an optimized version, please see the next post by timto.
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("FingerGestures")]
[Tooltip("Drag object")]
public class FingerGesturesDragCamera : FsmStateAction
{
[UIHint(UIHint.Variable)]
[Tooltip("GameObject container that has the Camera GameObject inside of it.")]
public FsmOwnerDefault cameraToDrag;
[UIHint( UIHint.Variable )]
[Tooltip( "The finger index dragging the object" )]
public FsmInt fingerIndex;
[Tooltip( "object being followed is a camera" )]
public FsmBool objectIsCamera;
[Tooltip( "left boundry for camera" )]
public FsmFloat minBoundryX;
[Tooltip( "right boundry for camera" )]
public FsmFloat maxBoundryX;
[Tooltip( "bottom boundry for camera" )]
public FsmFloat minBoundryY;
[Tooltip( "top boundry for camera" )]
public FsmFloat maxBoundryY;
private Space space;
public override void Reset()
{
cameraToDrag = null;
fingerIndex = null;
objectIsCamera = false;
space = Space.World;
minBoundryX = 0.0f;
minBoundryY = 0.0f;
maxBoundryX = 0.0f;
maxBoundryY = 0.0f;
}
public override void OnUpdate()
{
if( fingerIndex.IsNone || cameraToDrag.GameObject.IsNone || !cameraToDrag.GameObject.Value )
return;
FingerGestures.Finger finger = FingerGestures.GetFinger( fingerIndex.Value );
Transform tf = cameraToDrag.GameObject.Value.transform; // transform of the gameobject to drag
Vector3 currentFingerPos3d = ProjectOnCameraPlane( finger.Position, tf.position );
Vector3 previousFingerPos3d = ProjectOnCameraPlane( finger.PreviousPosition, tf.position );
Vector3 move = currentFingerPos3d - previousFingerPos3d;
// get the camera
var go = Fsm.GetOwnerDefaultTarget(cameraToDrag);
// get the position of the camera
var cameraPosition = space == Space.World ? go.transform.position : go.transform.localPosition;
// don't move camera left if camera is at the minimum X boundry
// don't move camera right if camera is at the maximum X boundry
if (((cameraPosition.x <= minBoundryX.Value) && (currentFingerPos3d.x > previousFingerPos3d.x)) ||
((cameraPosition.x >= maxBoundryX.Value) && (currentFingerPos3d.x < previousFingerPos3d.x)))
{
move.x = 0.0f;
}
// don't move camera down if camera is at the minimum X boundry
// don't move camera up if camera is at the maximum X boundry
if (((cameraPosition.y <= minBoundryY.Value) && (currentFingerPos3d.y > previousFingerPos3d.y)) ||
((cameraPosition.y >= maxBoundryY.Value) && (currentFingerPos3d.y < previousFingerPos3d.y)))
{
move.y = 0.0f;
}
// the specific amount to move the camera (for example, 25 points left)
tf.position -= move;
}
Vector3 ProjectOnCameraPlane( Vector2 screenPos, Vector3 refPos )
{
Camera cam = Camera.main;
Transform camtf = cam.transform;
// create a plane passing through the dragged object and facing toward the camera
Plane plane = new Plane( -camtf.forward, refPos );
Ray ray = cam.ScreenPointToRay( screenPos );
float t = 0;
if( !plane.Raycast( ray, out t ) )
return refPos;
return ray.GetPoint( t );
}
}
}
To use:
1. download attached CS file and drag into your PlayMaker actions directory in your game.
2. Create four global floats: min x, max x, min y, max y. (you'll use these in multiple fsms)
3. Create an empty gameobject (container) and put your camera gameobject inside of it.
4. Create an FSM on the container that checks to see if the camera's boundaries have been hit.
5. Create a drag manager FSM that deals with the moving the camera.
NOTE: The FSM attached to the camera container should be turned off by default and enabled in the drag manager FSM. If you don't, the camera will jerk when it hits a boundry. I'm not sure why enabling the FSM during gameplay fixes this, so if you know why, let me know!
Example Camera settings:
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-settings-1.pngExample Camera Container settings:
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm11.pnghttp://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm2.pnghttp://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm1.pngExample Drag Manger settings:
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager1.pnghttp://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager2.pnghttp://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager2-cont.pnghttp://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager3.pngI hope this helps!