Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: terrymorgan on December 26, 2012, 08:55:12 AM
-
I'm looking for a way to get my bearings on a big terrain, searching gave me this:
Use a GUITExt or GUI.Label (see script reference for examples)
eg
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), player.transform.position.ToString());
}
It doesn't work, someone said you need to set a variable. 'player'? Probably no need for FSM here.
-
I don't quite get what you're trying to do. Screen coordinates are 2D vectors which describe a point on the camera/final render.
Your code however draws a gui label which shows the position of the player. It's a combination of different functions in one, so you can't just use one action in playmaker for it.
this part
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20));
}
creates a Gui label of the position/size of (10, 10, 100, 20) .
In playmaker we use the "Gui Label" function for this. The text will be a string variable which we'll put our screen coordinates in. The style should be "label" by default. Screen Rect is irrelevant since you can set the position/size below. Turn normalized off if you want to define the size/position in pixels rather than percentage (1 = 100%).
Then as for the other part
player.transform.position.ToString()
that defines the text of the label. player is a custom variable in that script, which we need to create too. Put into that player variable your player gameObject.
.transform.position
this gets the players position. So just use a "Get Position" action on the player variable. and save the position as X, Y and Z floats. (We can't convert a vector3 to string like the programmer did in that piece of code)
.ToString()
Well, that part is pretty obvious. Create 3 new string variables called "X string" "Y string" and "Z string" and use "Float To String" actions to convert the floats to their respective strings.
Now, playmaker can't use 3 different strings in one GuiLabel, so we'll combine them. Create a new string variable called "Current Position String" and use a "Build String" action to combine your X string, Y string and Z string to the Current Position String. You can use a Separator like / to make it look like 0/0/0 .
Then you just use the Current Position String as the Text on the Gui Label Action.
Now it shows your position as a gui label. To update your position frequently, mark everyFrame on the actions (GuiLabel updates automatically).
Is that what you were asking for?
To make this work as a script, just do something like this:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject player;
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), player.transform.position.ToString());
}
}
The public GameObject player should now be definable if you drag the script onto any gameObject in the scene.
-
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject player;
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), player.transform.position.ToString());
}
}
Thanks Sven but I get
Assets/Scripts/GUI/screen_coords2.js(1,6): UCE0001: ';' expected. Insert a semicolon at the end.
It is a .js and not a .cs script? Anyway I don't see where to add any more semicolons.
My terrain map goes from 0,0 x and z to about 2000 , 2000 at the upper right. I need a 'sextant'
app that will display 259, 202, 636 (z is also included) if the player is at that position on the map. Doing it
with a keypress would be even better.
-
terribly sorry, I apparently hadn't really looked at it, it was a javascript. Here's the c# version:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public GameObject player;
void OnGUI () {
GUI.Label (new Rect (10, 10, 100, 20), player.transform.position.ToString());
}
}
in javascript you would do it like
var player : GameObject;
in your code that would be
var player : UnityEngine.GameObject;
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), player.transform.position.ToString());
}
(jesus christ, just looking at it I must've been half asleep when I wrote that :D Again, sorry)