Playmaker Forum
PlayMaker Updates & Downloads => Share New Actions => Topic started by: Lane on December 24, 2013, 10:20:09 AM
-
For getting the mouse position (Input.mousePosition) and storing it in a Vector3. Good for simplifying some common uses instead of using Get Mouse X and Get Mouse Y.
Options:
- Every Frame
- Invert Y (useful if you're using it for GUI)
- From Center of Screen
- Normalize
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GUI)]
[Tooltip("Get Mouse Position in screen space.")]
public class GetMousePosition : FsmStateAction
{
[RequiredField]
[Tooltip("Store the position in this Vector3")]
public FsmVector3 storeMousePosition;
[Tooltip("Do this every frame?")]
public bool everyFrame;
[Tooltip("Get the position relative to the center of the screen? (note: doesn't work with Invert Y checked)")]
public bool fromScreenCenter;
[Tooltip("Invery Y. For use with GUI space.")]
public bool invertY;
[Tooltip("Normalize the Vector3 result?")]
public bool normalized;
public override void Reset()
{
storeMousePosition = new FsmVector3 { UseVariable = true };
everyFrame = false;
fromScreenCenter = false;
normalized = false;
invertY = false;
}
public override void OnEnter()
{
DoGetPosition();
if (!everyFrame)
{
Finish();
}
}
public override void OnUpdate()
{
DoGetPosition();
}
void DoGetPosition()
{
var mousePos = Input.mousePosition;
if (invertY)
{
mousePos.y = Screen.height - mousePos.y;
}
if (fromScreenCenter)
{
mousePos.x -= Screen.width/2;
mousePos.y -= Screen.height/2;
}
if (normalized)
{
mousePos.x /= Screen.width;
mousePos.y /= Screen.height;
}
storeMousePosition.Value = mousePos;
}
}
}
-
Great action, thanks a lot Lane!
-
This is super useful and not easy to find, bumping!