playMaker

Author Topic: Follow Mouse[SOLVED]  (Read 10375 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Follow Mouse[SOLVED]
« on: October 30, 2012, 03:46:27 PM »
The script in this post works for 2D games on X/Y axis. Jean has an answer for 3D games a few posts down.

Use this script to have an object on screen follow the mouse. You can:
-give the object an X & Y offset from the mouse
-determine if the object can go off the screen
-if object can't go off screen, provide a screen offset that determines how close the object can get to the edge of the screen.

This script was designed for in-game objects. If you need something for your GUI, I would check out Jean's script here: http://hutonggames.com/playmakerforum/index.php?topic=2090.0

Follow Mouse
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Have an object follow the mouse.")]
public class FollowMouse : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetX;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetY;

[Tooltip("Select this if you don't want the object to move off screen with the mouse.")]
public FsmBool keepOnScreen;

[Tooltip("This determines how close to the edge of the screen the object can get IF Keep On Screen is used.")]
public FsmFloat screenOffset;

private float screenHeight;
private float screenWidth;

public override void Reset()
{
gameObject = null;
offsetX = null;
offsetY = null;
keepOnScreen = true;
screenOffset = null;
}

public override void OnEnter()
{
GetScreenSize();
DoSetPosition();
}

public override void OnUpdate()
{
DoSetPosition();
}

void GetScreenSize()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
}

void DoSetPosition()
{
if (Camera.main == null)
{
LogError("No MainCamera defined!");
Finish();
return;
}


var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

// get screen position
var position = Vector3.zero;
position.x = Input.mousePosition.x + offsetX.Value;
position.y = Input.mousePosition.y + offsetY.Value;

// if you want to keep the object on screen, this keeps the objects inside the screen
if (keepOnScreen.Value)
{
var xleft = 0f + screenOffset.Value;
var xright = screenWidth - screenOffset.Value;
var ytop = 0f + screenOffset.Value;
var ybottom = screenHeight - screenOffset.Value;

if (position.x < xleft) position.x = xleft;

if (position.x > xright) position.x = xright;

if (position.y < ytop) position.y = ytop;

if (position.y > ybottom) position.y = ybottom;
}

// convert screen position to world position
position = Camera.main.ScreenToWorldPoint(position);
position.z = 0f;

// move game object to world position
go.transform.position = position;
}

}



}
« Last Edit: February 17, 2015, 04:03:00 AM by jeanfabre »

rojosedano

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Follow Mouse
« Reply #1 on: October 30, 2012, 07:55:59 PM »
Hi, could you give an example or explain the use of this script? not get it to work.

thanks

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: Follow Mouse
« Reply #2 on: October 31, 2012, 02:30:12 PM »
My bet is that your Get Mouse X and Get Mouse Y are causing the freeze. This happened with my game. I've added four things to the script: screen x pos, screen y pos, world x pos, world y pos. Can you let me know if this fixes the problem? With these extra variables, you won't need Get Mouse X & Y.

Here is the script:

Follow Mouse + Get World X&Y + Get Screen X&Y
Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Have an object follow the mouse.")]
public class FollowMouse : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetX;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetY;

[Tooltip("Select this if you don't want the object to move off screen with the mouse.")]
public FsmBool keepOnScreen;

[Tooltip("This determines how close to the edge of the screen the object can get IF Keep On Screen is used.")]
public FsmFloat screenOffset;

[UIHint(UIHint.Variable)]
[Tooltip("The world X coordinate of the mouse.")]
public FsmFloat worldX;

[Tooltip("The world Y coordinate of the mouse.")]
[UIHint(UIHint.Variable)]
public FsmFloat worldY;

[Tooltip("The screen X coordinate of the mouse.")]
[UIHint(UIHint.Variable)]
public FsmFloat screenX;

[Tooltip("The screen Y coordinate of the mouse.")]
[UIHint(UIHint.Variable)]
public FsmFloat screenY;

private float screenHeight;
private float screenWidth;

public override void Reset()
{
gameObject = null;
offsetX = null;
offsetY = null;
keepOnScreen = true;
screenOffset = null;

worldX = null;
worldY = null;
screenX = null;
screenY = null;
}

public override void OnEnter()
{
GetScreenSize();
DoSetPosition();
}

public override void OnUpdate()
{
DoSetPosition();
}

void GetScreenSize()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
}

void DoSetPosition()
{
if (Camera.main == null)
{
LogError("No MainCamera defined!");
Finish();
return;
}


var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

// get screen position
var position = Vector3.zero;
position.x = Input.mousePosition.x + offsetX.Value;
position.y = Input.mousePosition.y + offsetY.Value;

if (!screenX.IsNone) screenX.Value = position.x;
if (!screenY.IsNone) screenY.Value = position.y;

// if you want to keep the object on screen, this keeps the objects inside the screen
if (keepOnScreen.Value)
{
var xleft = 0f + screenOffset.Value;
var xright = screenWidth - screenOffset.Value;
var ytop = 0f + screenOffset.Value;
var ybottom = screenHeight - screenOffset.Value;

if (position.x < xleft) position.x = xleft;

if (position.x > xright) position.x = xright;

if (position.y < ytop) position.y = ytop;

if (position.y > ybottom) position.y = ybottom;
}

// convert screen position to world position
position = Camera.main.ScreenToWorldPoint(position);
position.z = 0f;

// get world position
if (!worldX.IsNone) worldX.Value = position.x;
if (!worldY.IsNone) worldY.Value = position.y;

// move game object to world position
go.transform.position = position;
}

}

}

rojosedano

  • Playmaker Newbie
  • *
  • Posts: 15
Re: Follow Mouse
« Reply #3 on: November 01, 2012, 12:30:57 AM »
position in the world X and Y do not move and remain at 0 and 1.
The object does not move.

thanks!!!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Follow Mouse
« Reply #4 on: November 01, 2012, 07:19:14 AM »
Hi,

 Indeed, the script can not work as is. you need to stipulate the z distance from the camera, here, it's 0, therefore where the camera is exactly, so it's not moving at all

try this script, which is the same with a new variable to define the distance from the camera.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Have an object follow the mouse.")]
public class FollowMouse : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetX;

[Tooltip("Show the object a specific distance on the X axis, away from the mouse.")]
public FsmFloat offsetY;

[Tooltip("Select this if you don't want the object to move off screen with the mouse.")]
public FsmBool keepOnScreen;

[Tooltip("This determines how close to the edge of the screen the object can get IF Keep On Screen is used.")]
public FsmFloat screenOffset;

public FsmFloat distanceToCamera;


private float screenHeight;
private float screenWidth;


public override void Reset()
{
gameObject = null;
offsetX = null;
offsetY = null;
keepOnScreen = true;
screenOffset = null;
distanceToCamera = 4f;

}

public override void OnEnter()
{

GetScreenSize();
DoSetPosition();
}

public override void OnUpdate()
{
DoSetPosition();
}

void GetScreenSize()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
}

void DoSetPosition()
{
if (Camera.main == null)
{
LogError("No MainCamera defined!");
Finish();
return;
}


var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

// get screen position
var position = Vector3.zero;
position.x = Input.mousePosition.x + offsetX.Value;
position.y = Input.mousePosition.y + offsetY.Value;

// if you want to keep the object on screen, this keeps the objects inside the screen
if (keepOnScreen.Value)
{
var xleft = 0f + screenOffset.Value;
var xright = screenWidth - screenOffset.Value;
var ytop = 0f + screenOffset.Value;
var ybottom = screenHeight - screenOffset.Value;

if (position.x < xleft) position.x = xleft;

if (position.x > xright) position.x = xright;

if (position.y < ytop) position.y = ytop;

if (position.y > ybottom) position.y = ybottom;
}


position.z = distanceToCamera.Value;

// move game object to world position
go.transform.position = Camera.main.ScreenToWorldPoint(position);
}

}



}


Bye,

 Jean

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: Follow Mouse
« Reply #5 on: November 01, 2012, 07:14:41 PM »
Ah yes, I see why it wasn't working now. I'm working on a 2D game, so Z never changes. I'll make a note at the top.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Follow Mouse
« Reply #6 on: February 04, 2015, 04:10:30 AM »
Hi,

 Added this followMouse action to the Ecosystem.

 you can download it here directly as well:

FollowMouse download

FollowMouse sourcecode preview

 Bye,

 Jean

PlaymakerNOOB

  • Full Member
  • ***
  • Posts: 219
Re: Follow Mouse
« Reply #7 on: February 11, 2015, 10:39:38 PM »
This is excellent!  Does exactly what I need. 
« Last Edit: February 11, 2015, 10:43:22 PM by PlaymakerNOOB »

psypol

  • Playmaker Newbie
  • *
  • Posts: 39
Re: Follow Mouse[SOLVED]
« Reply #8 on: January 22, 2016, 05:34:25 PM »
hello

i wanted to use this script for a proto that i playtest on PC and Mobile Android aswell

the script works fine on Pc and on device
UNTIL you touch the screen with a second finger
at this moment instead of snapping on the position of the first or the second finger ... the object always follows the exact position inbetween the 2 fingers touching the screen.

is there a way to avoid this ?
either by changing something in the script or maybe some way to deactivate the multi-touch (i dont plan to use it at all in this project) ?

thanks

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Follow Mouse[SOLVED]
« Reply #9 on: May 25, 2016, 04:08:00 AM »
Hi,

 this is the actually Unity behaviour when mouse is emulated using fingers, it gets the center of all positions.

if you are indeed going touch based input, you should then implement a proper solution for touches, this action is really meant to be used with mouse, and works with touches because Unity provides a simple bridge to it, but it isn't ideal in all cases as you found out.

Bye,

 Jean