playMaker

Author Topic: Get Mouse Position  (Read 5673 times)

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2508
  • Mender of the past
    • View Profile
    • Cleverous
Get Mouse Position
« 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

Code: [Select]
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;
}
}
}
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

Nitrohex

  • Junior Playmaker
  • **
  • Posts: 50
    • View Profile
Re: Get Mouse Position
« Reply #1 on: December 24, 2013, 11:44:58 AM »
Great action, thanks a lot Lane!
Sign Up Now @ StartApp.com

fromfame

  • Junior Playmaker
  • **
  • Posts: 75
  • Sloppy PM Veteran
    • View Profile
Re: Get Mouse Position
« Reply #2 on: August 18, 2021, 01:12:44 PM »
This is super useful and not easy to find, bumping!