Hi chaps. Okay, I’m wondering why my text isnt appearing right over my 3d object…heres the code…
private var guiTextObject :GUIText;
var textOffsetFromMesh: Vector3;
private var newPosition:Vector3;
var color:Color;
var guiTextObjectXPosition:float;
var guiTextObjectYPosition:float;
function Start () {
guiTextObject=GetComponent(GUIText);//this is this objects own gui text
//guiTextObject.text.Color.black;
guiTextObject.material.color = color;
}
function OnGUI () {
//gets the object world coordinates and puts the 2d text at those coords in 2d space
newPosition= Camera.main.WorldToViewportPoint(transform.position);//from world to viewport
guiTextObject.transform.position=newPosition+textOffsetFromMesh;
guiTextObject.material.color = color;
guiTextObjectXPosition=guiTextObject.transform.position.x;
guiTextObjectYPosition=guiTextObject.transform.position.y;
}
Basically the mesh is there but not the 2d GUIText text…ANy ideas?
Don’t set the transform.position as that will provide wild/odd results. Instead you should be setting/manipulating your GUIText object’s pixel offset values. Switch to that and you should get much better results.
private var guiTextObject :GUIText;
private var newPosition:Vector2;
var color:Color;
var guiTextObjectXPosition:float;
var guiTextObjectYPosition:float;
function Start () {
guiTextObject=GetComponent(GUIText);//this is this objects own gui text
//guiTextObject.text.Color.black;
guiTextObject.material.color = color;
}
function OnGUI () {
//gets the object world coordinates and puts the 2d text at those coords in 2d space
newPosition= Camera.main.WorldToViewportPoint(transform.position);//from world to viewport
guiTextObject.pixelOffset=newPosition;
guiTextObject.material.color = color;
guiTextObjectXPosition=guiTextObject.transform.position.x;
guiTextObjectYPosition=guiTextObject.transform.position.y;
}
Read that doc page, adjust your value settings appropriately and try again.
The transform.position of the GUIText/GUITexture objects is nearly irrelevant except the z-value which determines layering orders. You really don’t need to store/reference/adjust those for these purposes.
Afraid I have to disagree with that; I always use transform.position for setting the position of GUIText/GUITextures. I would be using pixelInset if I was interested in pixel-exactness, but I’m not, as IMO that’s nearly useless for games, and I’m far more interested in resolution-independence, where e.g. “transform.position.x = .25” is 25% of the way across the screen on any system.
A few things with the code: I’m assuming you’ve left out some OnGUI code that has to do with the “guiTextObjectX/YPosition” variables…if not, then don’t use OnGUI; that’s for Unity’s GUI.x system, which has nothing to do with GUITextures or GUIText, and will only slow things down for no reason in this case.
“guiTextObject = GetComponent(GUIText);” can just be “guiTextObject = guiText;” It’s the same thing behind the scenes, but simpler the second way.
The main problem seems to be that you’re using the same object for world position as you are for the GUIText. These have to be different objects, since the coordinate system is different.
Cheer guys. I think that the fact the GUitext and the mesh object were part of the same game object could be the problem, but I have since split tham up into separate objects and changed the code to…
var guiTextObject :GUIText;
var textOffsetFromMesh: Vector3;
private var newPosition:Vector3;
var color:Color;
var guiTextObjectXPosition:float;
var guiTextObjectYPosition:float;
function Start() {
guiTextObject.material.color = color;
}
function Update() {
//gets the object world coordinates and puts the 2d text at those coords in 2d space
newPosition= Camera.main.WorldToViewportPoint(transform.position);//from world to viewport
guiTextObject.transform.position=newPosition+textOffsetFromMesh;
guiTextObject.material.color = color;
guiTextObjectXPosition=guiTextObject.transform.position.x;
guiTextObjectYPosition=guiTextObject.transform.position.y;
}
…but I still see no text.(Well I see it on screen in the preview window before the update is called.)This one has me a bit stumped !? :?
Erm…kay guys…it was something to do with setting the color in the end. (!!!) Basicaly I manually set it to black in code rather than using the color picker and it worked. Using the color picker as before meant the text was invisible. I dont understand why but here is the working code…
var guiTextObject :GUIText;
var textOffsetFromMesh: Vector3;
private var newPosition:Vector2;
var color:Color;
var guiTextObjectXPosition:float;
var guiTextObjectYPosition:float;
function Start() {
guiTextObject.material.color = color.black;
}
function Update() {
//gets the object world coordinates and puts the 2d text at those coords in 2d space
newPosition= Camera.main.WorldToViewportPoint(transform.position);//from world to viewport
guiTextObject.transform.position=newPosition+textOffsetFromMesh;
//guiTextObject.material.color = color;
}
But now I have a newew problem…
…although this works in theory, as my mesh object moves across the screen the text flickesr as it moves. I’ll do a search and see what comes up.
Thanks for your help!
Yeah it looks like you were right about the alpha.I didnt realise the default was alpha 0. :shock:
Regarding the flickering I have a second script which controls the sliding on screen of images(3d mesh) but I cant figure out how this would affect the text. It looks like the text wants to be in two different places as the slide in is taking place… I know I’m now going a little off topic but here’s the code.
I have tried removing the timedeltas and the yield statement but these seem to have no affect on the flicker…
The slide in code is as follows…
//slide in script
var delayBeforeStart:float=0;
var xspeed:float=-5;
var yspeed:float=0;
var maxX:int=0;
var maxY:int=0;
var horizontalSlide:boolean=true;
private var canMove:boolean=true;
function Update () {
if (canMove)
{
Move_me();
}
}
function Move_me()
{
yield WaitForSeconds(delayBeforeStart);
transform.Translate(xspeed*Time.deltaTime,yspeed*Time.deltaTime,0);
if(horizontalSlide)
{
if(xspeed>=0)//if its not moving in reverse
{
if (transform.position.x>maxX){transform.position.x=maxX;canMove=false;}
}
else
{
if (transform.position.x<maxX){transform.position.x=maxX;canMove=false;}
}
}
else
{
if(yspeed>=0)//if its not moving in reverse
{
if (transform.position.y>maxY){transform.position.y=maxY;canMove=false;}
}
else
{
if (transform.position.y<maxY){transform.position.y=maxY;canMove=false;}
}
}
Any assistance in this matter is greatly recieved. Thanks!
The flicker was caused because the guiText was a child of the mesh object that is was following. As soon as I turned them into two separate entities(un related) within the hierachy pane the flickering stopped.
I’m not sure why or how, but I’m happy for the time being
That would be because GUIText/GUITextures use viewport space for the transform.position, as opposed to world space, so yeah they have to be separate objects.