Last active
December 10, 2015 12:48
-
-
Save MattRix/4436375 to your computer and use it in GitHub Desktop.
This class allows you to link the position of a Unity GameObject to a Futile FNode. You should set METERS_TO_POINTS to a value that makes sense for your current game.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System; | |
public class FNodeLink : MonoBehaviour | |
{ | |
public static float METERS_TO_POINTS = 64.0f; | |
public static float POINTS_TO_METERS = 1.0f/METERS_TO_POINTS; | |
private FNode _node; | |
private bool _shouldLinkRotation; | |
private bool _shouldUseLocalPosition = false; | |
public void Init(FNode node, bool shouldLinkRotation) | |
{ | |
_node = node; | |
_shouldLinkRotation = shouldLinkRotation; | |
Update(); | |
} | |
public void Update() | |
{ | |
if (_node == null) | |
{ | |
Debug.Log("_node is null for GameObject: " + gameObject.name); | |
} | |
if(_shouldUseLocalPosition) | |
{ | |
_node.x = gameObject.transform.localPosition.x*METERS_TO_POINTS; | |
_node.y = gameObject.transform.localPosition.y*METERS_TO_POINTS; | |
} | |
else | |
{ | |
_node.x = gameObject.transform.position.x*METERS_TO_POINTS; | |
_node.y = gameObject.transform.position.y*METERS_TO_POINTS; | |
} | |
if(_shouldLinkRotation) | |
{ | |
_node.rotation = -gameObject.transform.rotation.eulerAngles.z; | |
} | |
} | |
public FNode node | |
{ | |
get {return _node;} | |
} | |
public bool shouldLinkRotation | |
{ | |
get {return _shouldLinkRotation;} | |
set | |
{ | |
if(_shouldLinkRotation != value) | |
{ | |
_shouldLinkRotation = value; | |
if(_shouldLinkRotation) Update(); | |
} | |
} | |
} | |
public bool shouldUseLocalPosition | |
{ | |
get {return _shouldUseLocalPosition;} | |
set | |
{ | |
if(_shouldUseLocalPosition != value) | |
{ | |
_shouldUseLocalPosition = value; | |
if(_shouldUseLocalPosition) Update(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment