- Home /
3d text backwards when facing camera
I am new to this forum but have been using Unity for a while now. I have made alot of progress with the game I am working on so far but am having a hangup on this one piece.
var Enemy: Transform;
var distance = 5;
var Explode : Transform;
var circle : Transform;
var projectile : Rigidbody;
//sounds for attacking
var SmiteSound: AudioClip;
//background texture
var attack : Texture2D;
var smite : Texture2D;
//textures for buttons
var xout : Texture2D;
var wasClicked: boolean = false;
//arrow shooting
var speed = 5.0;
var rotateSpeed = 3.0;
//health
var DamageNumber : int;
var hp = 100.0;
var HitPoints = 100.0;
var ScorePoints = 2.0;
var track_speed : float = 2;
var look_at : Quaternion;
var enemies_list = new GameObject[10];
var aiming_at : int = 0;
var aiming : boolean = false;
var cameraToUse : Camera;
var SetPlayerName : String;
var TextMeshName : GameObject;
var style : GUIStyle;
//selection circle disabled
circle.renderer.enabled = false;
aiming = false;
function Start(){
style.normal.textColor = Color.yellow;
style.fontSize = 15;
DamageNumber = Random.Range(1,(HitPoints/6));
}
function OnMouseUp() {
wasClicked = true; // turns on button.
circle.renderer.enabled = true;
if(aiming == false)
{
update_list();
if (enemies_list.length > 0) // If we don't have enemies, we don't need to do the quaternion code
aiming = false; // for performance reasons.
}
}
function OnGUI() {
GUI.depth = 50;
if (wasClicked) {
wasClicked=true;
//show inventory bar
GUI.BeginGroup (Rect (Screen.width / 2 - 450, Screen.height - 49, 900, 49));
if(GUI.Button (Rect (43,8,39,40), attack, GUIStyle.none)){
//BuffPlate.active=true;
audio.PlayOneShot(SmiteSound);
//instantiate the projectile
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
//move the projectile
instantiatedProjectile.velocity = (enemies_list[aiming_at].GetComponent(Transform).position - transform.position)*speed;
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
DamageNumber = Random.Range(1,(HitPoints/6));
print ("hit : " + aiming_at + " with " + DamageNumber + " damage");
hp = hp -= DamageNumber;
Destroy(instantiatedProjectile.gameObject, 5);
}
if(GUI.Button (Rect (79,8,39,40), smite,GUIStyle.none)){
}
if (GUI.Button(new Rect(408, 8, 39, 40), xout,GUIStyle.none)) {
circle.renderer.enabled = false;
wasClicked = false; // turns off button.
}
GUI.EndGroup ();
}
}
function Update(){
SetPlayerName = Enemy.name;
TextMeshName.gameObject.GetComponent(TextMesh).text = SetPlayerName + "\n\r" + hp;
if (Input.GetKeyDown("1")) {
if (wasClicked) {
wasClicked=true;
audio.PlayOneShot(SmiteSound);
//instantiate the projectile
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
//move the projectile
instantiatedProjectile.velocity = (enemies_list[aiming_at].GetComponent(Transform).position - transform.position)*speed;
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
// Send a damage message to the hit object
DamageNumber = Random.Range(1,(HitPoints/6));
print ("hit : " + aiming_at + " with " + DamageNumber + " damage");
hp = hp -= DamageNumber;
Destroy(instantiatedProjectile.gameObject, ScorePoints);
//print ("aiming at : "+aiming_at);
}
}
if(aiming == true)
{
look_at = Quaternion.LookRotation(enemies_list[aiming_at].GetComponent(Transform).position - transform.position);
look_at.x = 0;
look_at.z = 0;
transform.rotation = Quaternion.Lerp(transform.rotation, look_at, track_speed);
// This will make the character turn to the enemy
// The next part handles the switching between enemies
if (Input.GetKeyDown("-"))
{
if (aiming_at < enemies_list.length)
aiming_at++;
else
aiming_at = 0;
}
if (Input.GetKeyDown("="))
{
if (aiming_at > 0)
aiming_at--;
else
aiming_at = enemies_list.length -1;
}
}
if (hp <= 0)
Destroy(gameObject);
if (hp <= 0)
{
print ("scores: " + PlayerPrefs.GetString("playerName") + "--" + ScorePoints);
//Instantiate(Explode, transform.position, transform.rotation);
postScore(PlayerPrefs.GetString("playerName"),ScorePoints);
}
}
function update_list(){
enemies_list = GameObject.FindGameObjectsWithTag("Enemy");
}
function postScore(name, score) {
//var hash=Md5.Md5Sum(name + score + secretKey);
var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score;
// Post the URL to the site and create a download object to get the result.
hs_post = WWW(highscore_url);
yield hs_post; // Wait until the download is done
if(hs_post.error) {
print("There was an error posting the high score: " + hs_post.error);
}
}
@script RequireComponent(AudioSource)
This piece of code allows me to select the NPC, show the attack buttons, and calculate the death, then destroy and update scoring.
the problem is that the 3d text is backwards and not facing the camera. I did use the CameraFacingBillboard.cs example HERE and now the 3d text faces the camera as I walk around the NPC. However, the text is backwards and I cannot figure out how to flip it around. I am also using Unity 4 and a lot of the examples that I see are for Unity 3. Does this make a difference when doing the scripting?
the specific place the name of the NPC is placed is the first two lines of the update function.
SetPlayerName = Enemy.name;
TextMeshName.gameObject.GetComponent(TextMesh).text = SetPlayerName + "\n\r" + hp;
I have tried to do several examples and nothing seems to work, or breaks the code. Can anyone help?
Thanks.
Where is the relevant part in your script dump that uses 3D text and/or the billboard? Don't just dump tons of code on us that are unrelated or irrelevant.
the part that says:
SetPlayerName = Enemy.name; Text$$anonymous$$eshName.gameObject.GetComponent(Text$$anonymous$$esh).text = SetPlayerName + "\n\r" + hp;
that is the 3d text portion. I included the whole script to show what I was doing in total.
Ah, didn't realise these two lines were part of that script. Have you tried my answer?
Answer by Wolfram · Jan 12, 2013 at 12:41 AM
Replace Vector3.back
in your CameraFacingBillboard script with Vector3.forward
(assuming you're using the non-modded version).
thanks for at. That worked like a charm... Now to change the color of the 3d text...
For reference: this is not a clean solution, but the quickest solution. However, you might run into similar problems if you now want to use the billboard script for other objects (e.g., Unity planes, other objects).
The root of the problem is that the billboard script has to pick one axis and orient that towards the camera. Generally that should be the local +Z axis of the object, as this is generally considered the "forward" axis of an object in Unity. If the object doesn't adhere to that convention (as the 3D text probably doesn't), you'd either have to pivot your object correctly first (i.e., attach the script to the parent of your object, then locally rotate your object to make it align along +Z of the parent) - or you could use the "hack" I provided, which works for this special case.
Answer by Julien-Lynge · Jan 11, 2013 at 07:09 PM
There's no difference between Unity 3 and Unity 4 with this kind of scripting. Most likely you'll have to modify the CameraFacingBillboard script to rotate the text 180 degrees around the Y axis. Take a look at the Transform script reference - there are a couple ways to rotate objects, and there is example code for each.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
thanks for noting that there is no differece between the versions. that is good to know... I am trying to get this figured out.. and the docs arent helping me at the moment. Still working on it though.
Your answer
Follow this Question
Related Questions
Helper AI follow character 0 Answers
How do I animate an 'enemy' NPC? 0 Answers
Issues NPC Grenade avoidance 3 Answers
Can unity3d combined with Artificial Intelligent or Soft Computing? 3 Answers
Simple airplane AI 2 Answers