- Home /
GUI to follow 3d instantiated object in other camera?
Hi Guys, having trouble getting a 2d math equation to show up in a secondary camera layer. The instantiated PowerUp object shows in the second layer, but the GUI, though a child object, appears in the camera.main. I'm using the same script to generate equations for the asteroid equations and the PowerUP equations, using a conditional to separate them out. Should I use and array of cameras for different GUI? Can I get a reference to a second camera on the fly like Camera.main ????? Not sure. Appreciate your brilliance and generosity.
if(m_quientGT)
{
m_quientGT.transform.position = Camera.main.WorldToViewportPoint(transform.position+quientOffset);
if (this.gameObject.tag == "PowerUp")
{
m_quientGT.transform.position = Camera.????????????.WorldToViewportPoint(transform.position+quientOffset);
}
}
Answer by robertbu · Dec 09, 2013 at 07:26 PM
There are lots of ways to accomplish this goal. You don't say what language. I'll assume Javascript/Unityscript:
At the top of the file:
private var secondCamera : Camera;
In Start():
secondCamera = GameObject.Find("SecondCameraName").camera;
In your code above:
m_quientGT.transform.position = scecondCamera.WorldToViewportPoint(transform.position+quientOffset);
As a bit less efficient alternate, give your camera a unique tag then you can do:
m_quientGT.transform.position = GameObject.FindWithTag("SecondCameraTag").camera.WorldToViewportPoint(transform.position+quientOffset);
Thanks for your help! This solution works sufficiently well. I was worried that it might be too expensive because so many asteroids get spawned and so few power-ups relatively. The added conundrum is that a laser targets the power-up, but does so in "real world" positioning, so that if the main camera/player is in a bizarre position––while the power-up looks awesome in the scene--the laser shoots it somewhere else....I may simply have to animate to the main camera instantiating the object relative to its position. Thanks again!