playMaker

Author Topic: Get Screen Position / Set Screen Position  (Read 5792 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Get Screen Position / Set Screen Position
« on: October 29, 2012, 03:23:03 PM »
First script gets the position of a game object in world space and converts it to screen space. The second script sets an object at a position on the screen.

There are actions that already do this, but they require a combination of actions. This keeps things simple.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Gets the Position of a Game Object on the screen and stores it in Float Variables")]
public class GetScreenPosition : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("Store the screen X position in a Float Variable.")]
public FsmFloat screenX;

[Tooltip("Store the screen Y position in a Float Variable.")]
public FsmFloat screenY;

public bool everyFrame;

public override void Reset()
{
gameObject = null;
screenX = null;
screenY = null;
everyFrame = false;
}

public override void OnEnter()
{
DoGetPosition();

if (!everyFrame)
{
Finish();
}
}

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

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


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

// store the world position
var position = go.transform.position;

// store the screen position
position = Camera.main.WorldToScreenPoint(position);
screenX.Value = position.x;
screenY.Value = position.y;
}


}
}


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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Converts the screen position of a Game Object into the world position.")]
public class SetScreenPosition : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("Enter the the vector 3 that has your screen coordinates. If you don't have this, set each coordinate below.")]
public FsmVector3 vector3Value;

[Tooltip("Enter the screen X position.")]
public FsmFloat screenX;

[Tooltip("Enter the screen Y position.")]
public FsmFloat screenY;

[Tooltip("Enter the world Z position.")]
public FsmFloat worldZ;

public bool everyFrame;

public override void Reset()
{
gameObject = null;
vector3Value = null;
screenX = null;
screenY = null;
worldZ = null;
everyFrame = false;
}

public override void OnEnter()
{
DoSetPosition();

if (!everyFrame)
{
Finish();
}
}

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

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


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

var position = Vector3.zero;

// get the screen position
if (vector3Value == null)
{
position.x = screenX.Value;
position.y = screenY.Value;
}
else
{
position = vector3Value.Value;
}

// convert screen position to world position
position = Camera.main.ScreenToWorldPoint(position);
position.z = worldZ.Value;

// move game object to world position
go.transform.position = position;
}
}
}
« Last Edit: October 30, 2012, 02:29:31 PM by amaranth »