playMaker

Author Topic: Consolidated Get Mouse X and Y  (Read 1866 times)

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • View Profile
    • Disastercake
Consolidated Get Mouse X and Y
« on: May 26, 2012, 11:45:14 AM »
I feel like the get mouse X and Y actions should be consolidated into one...  So I did it. =P

This will not require either, so if you don't put in a store variable for one, it just won't bother to store that one, but will still store the other.

Also it properly does every frame, since (as of 2012-05-26) the current get mouse x and y actions that are packaged with playmaker have a small bug in them that doesn't allow for them to be checked every frame.

Code: [Select]
using System;
using System.Collections;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions{

[ActionCategory("Custom Actions")]
[Tooltip("Gets the X and Y Position of the mouse and stores it in two Float Variables.")]
public class GetMouseXAndY : FsmStateAction
{
//[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat mouseXPosition;
//[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat mouseYPosition;
public bool normalize;
public bool everyFrame;

public override void Reset()
{
mouseXPosition = null;
mouseYPosition = null;
normalize = true;
everyFrame = false;
}

public override void OnEnter()
{
DoGetMouseX();
DoGetMouseY();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoGetMouseX();
DoGetMouseY();
}

void DoGetMouseX()
{
if (mouseXPosition != null)
{
float xpos = Input.mousePosition.x;

if (normalize)
xpos /= Screen.width;

mouseXPosition.Value = xpos;
}
}

void DoGetMouseY()
{
if (mouseYPosition != null)
{
float ypos = Input.mousePosition.y;

if (normalize)
ypos /= Screen.height;

mouseYPosition.Value = ypos;
}
}
}
}
« Last Edit: May 26, 2012, 11:52:23 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com