playMaker

Author Topic: [Custom] FingerGestures Move 2D Camera with Mouse  (Read 4544 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
[Custom] FingerGestures Move 2D Camera with Mouse
« on: May 11, 2012, 11:26:45 AM »
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.
Code: [Select]
// (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.png

Example Camera Container settings:
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm11.png
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm2.png
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/camera-container-fsm1.png

Example Drag Manger settings:
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager1.png
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager2.png
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager2-cont.png
http://i781.photobucket.com/albums/yy94/agf8623/Screenshots/drag-manager3.png

I hope this helps!
« Last Edit: July 30, 2012, 12:15:33 PM by amaranth »

timto

  • Playmaker Newbie
  • *
  • Posts: 1
    • View Profile
Re: [Custom] FingerGestures Move 2D Camera with Mouse
« Reply #1 on: May 30, 2012, 02:13:33 PM »
The Draw Plane function did not work for me...
Couldnt update currentFingerPosition - kept giving me only the CamPosition.
Needed to move the camera in x and z Direction.
Thats why I assigned so strangely ;)
Why not make it small and compact?  :D

Code: [Select]
// (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;


public override void Reset()
{
            cameraToDrag = null;
            fingerIndex = null;
objectIsCamera = false;
}
       
        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


float newX = finger.PreviousPosition.x - finger.Position.x;
float newZ = finger.PreviousPosition.y - finger.Position.y;
float newY = 0;


//            Vector3 move = currentFingerPos3d - previousFingerPos3d;
Vector3 move = new Vector3(newX/10, newY, newZ/10);

tf.position -= move;
        }

}
}


amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Re: [Custom] FingerGestures Move 2D Camera with Mouse
« Reply #2 on: May 30, 2012, 04:04:46 PM »
Awesome, now we have two alternatives! :)

smiffy

  • Playmaker Newbie
  • *
  • Posts: 49
    • View Profile
Re: [Custom] FingerGestures Move 2D Camera with Mouse
« Reply #3 on: July 30, 2012, 08:28:08 AM »
Hi Amaranth thanks for the action! The image links are no longer working though, can you re-up them please?

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Re: [Custom] FingerGestures Move 2D Camera with Mouse
« Reply #4 on: July 30, 2012, 12:16:34 PM »
Links updated. :)

smiffy

  • Playmaker Newbie
  • *
  • Posts: 49
    • View Profile
Re: [Custom] FingerGestures Move 2D Camera with Mouse
« Reply #5 on: July 30, 2012, 12:32:40 PM »
Thankyou!