playMaker

Author Topic: Displaying screen coordinates  (Read 3215 times)

terrymorgan

  • Junior Playmaker
  • **
  • Posts: 65
Displaying screen coordinates
« 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.

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Displaying screen coordinates
« Reply #1 on: December 27, 2012, 09:00:29 AM »
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
Quote
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
Quote
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.
Quote
.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)
Quote
.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:

Code: [Select]
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.
Best,
Sven

terrymorgan

  • Junior Playmaker
  • **
  • Posts: 65
Re: Displaying screen coordinates
« Reply #2 on: December 30, 2012, 10:29:21 AM »
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.

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: Displaying screen coordinates
« Reply #3 on: December 30, 2012, 11:18:28 AM »
terribly sorry, I apparently hadn't really looked at it, it was a javascript. Here's the c# version:

Code: [Select]
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

Code: [Select]
    var player : GameObject;

in your code that would be

Code: [Select]
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)
« Last Edit: December 30, 2012, 11:30:47 AM by kiriri »
Best,
Sven