GUI Text Always Facing Camera

How can i have it where my 3D Text will always face the camera. Basically the 3d text is on top of the game object for my enemy and the gui text has a variable that calls it and changes its name to the correct name and such but how can i make it where the text always faces the camera no matter what way the NPC is looking? Maybe some sort of rotation, i have ideas and how it could be done i just dont know how to do it. And one more quick thing is that when i have something like this

selectTarget.renderer.material.color = Color.red;

It changes selectTarget (which is a variable) and makes it red, is there any other way without having to make another variable to select the 3d Text which selectTarget is a parent of and have it change the text color of the 3d text to red?

All help is appreciated, thanks

EDIT

Since this will be a multiplayer game cant we have it set up where it faces any of the cameras? So it faces me when i look at it (the 3d text) and faces my friend being it.

Create C# script and put this on the GUI text you want to always face you. I cant remember where I got it from but it works well. Use it probably for the same purpose as you want which it to have labels above other players / items.

using UnityEngine;

using System.Collections;

public class LookAtCameraYonly : MonoBehaviour

{

 public Camera cameraToLookAt;



void Start() 

{

	//transform.Rotate( 180,0,0 );

}



void Update() 

{

     Vector3 v = cameraToLookAt.transform.position - transform.position;

     v.x = v.z = 0.0f;

     transform.LookAt( cameraToLookAt.transform.position - v ); 

	 transform.Rotate(0,180,0);

}

}

Litte improvement if you want camera rotation too (not only position)

Vector3 v = Camera.mainCamera.transform.position - transform.position;

v.x = v.z = 0.0f;

transform.LookAt( Camera.mainCamera.transform.position - v );

transform.rotation =(Camera.mainCamera.transform.rotation); // Take care about camera rotation

Hi,

If you’re not concerned about depth, a simple way to do this is convert your character’s world space coordinates in to screen space (with a positive offset on the Y axis) and then render your text centred around this point. I have wrote an article about working with GUIText and fonts. If you click on the link at the bottom of the article it had a class with a method for rendering text from world space coordinates:

http://www.bytefoundry.co.uk/blog/?p=70

If you are concerned about depth you will need to use the same maths as in billboarding textures (do a Google search for billboard textures). If you have trouble I will get you an example tomorrow. I’m answering this from my mobile at the moment because I’m not home.

Solution #1

You could consider making the text with a GUI text instead, but then you would have to calculate the size and positon in 2D. Which is possible too.

Solution #2

If you just need to align a 3D object against another (aka the camera) then use the LookAt function. If it turns the oppersite direction, then just rotate/reverse the angle found afterwards.

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

You just need to run the script on each players computer, so the “face cam” script faces the player looking at the sign/text. I dont see a multiplayer problem in that.

Hope you will find this usefull?