- Home /
Having a 3D text appear in the middle of the screen without using GUI (C#)
Hi all,
For my game, I want to provide some text blocks with information. Because I'm going to use it with the Oculus Rift, I can't use GUI, so instead of using Canvas with Text children, I'm using 3D texts. For readability, I'm trying to get the text (when this is activated) to appear in the middle of the screen, no matter where the camera turns -- like you're using Screen Space - Overlay with a Canvas.
So far, I've found a script to attach to the 3D Text GameObject, which tracks the position of the camera (see code below). However, this isn't quite what I'm looking for as the text is still not stationary in the middle of the screen. I've tried using the same way of coding for tracking the camera rotation, but to no avail.
Is there anyone who could help me out? I'd greatly appreciate it!
public class LookAtText : MonoBehaviour {
public Camera cameraToLookAt;
void Start()
{
}
void Update()
{
Vector3 v = cameraToLookAt.transform.position - transform.position;
v.x = v.z = 0.0f;
transform.LookAt(cameraToLookAt.transform.position - v );
//transform.Rotate(Camera.main.transform.rotation);
transform.Rotate (0, 180, 0);
}
}
Worked like a charm, thank you so much! I can't believe I was trying to script it when the solution was so straightforward..
should answer as answers not comments. thread looks forever unanswered =O
Answer by KdRWaylander · May 20, 2015 at 11:13 AM
Hey, what you can do is put the text as a child of one of the cameras (left or right), set its position to (0,0,0) and then use local position to put the text as far as you want from the camera. In this way, it will always follow the movement of the camera ... look at this tutorial, you could adapt your way to it: https://www.youtube.com/watch?v=LLKYbwNnKDg
Answer by Owen-Reynolds · May 20, 2015 at 01:49 PM
Looks like you're just starting with positioning and rotating objects. Putting something in front of the camera, or facing the camera, is no different than putting something in front/facing the player, or a cube... . Reading through the basics should make various solution to this obvious.
As kdrwl writes, an easy way is to use local position. In the Editor, child an object to something and play with position (which automatically counts as localPosition, even though it doesn't say) to get a feel for it (same with rotation.) In code: myText.parent=someCamera; myText.localPosition=new Vector3(0,0,10);
. Since we all know Z is forward, this puts you 10 units along the camera's blue(Z) arrow. Then myText.LookAt(someCamera);
aims directly at it.
Can also use world positioning, involving someCamera.forward*10;
. It appears your last line is part of the trick "face the exact way as something, then spin around to face it." Whole thing is: myText.rotation=someCamera.rotation; myText.Rotate(0,180.0);
. But don't mix methods. There are a few ways to do it, none of them very long, but it's easy to use 1/2 of method A and 1/2 of method B, and get junk.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Create camera transition from one point to top of object 1 Answer
Runescape Camera Rotation? 0 Answers
Camera Follow Mouse 2 Answers