- Home /
An UI GameObject only visible on one side
Hello, I try to find a solution to make an UI Image or Text invisible on one side. My scene is in 2D and these GameObject need to be invisible when flipped (they rotate around X and Y axis).
SetActive(false) with the statement "if eulerAngles > 90 && < 270" doesn't work as it's a perspective projection, so 90° in the game doesn't mean 90° for the player.
Any solutions ? Thanks.
Answer by bpaynom · Jul 09, 2019 at 01:59 PM
[RequireComponent(typeof(Graphic))]
public class HideOnBack : MonoBehaviour
{
private Graphic renderer;
private Camera camera;
// Start is called before the first frame update
void Start()
{
renderer = GetComponent<Graphic>();
camera = Camera.main;
}
// Update is called once per frame
void Update()
{
float angle = Vector3.Angle( (transform.position - camera.transform.position) , transform.forward );
Debug.Log( "Angle: " + angle );
renderer.enabled = angle < 90;
}
}
It works great, thanks.
Just a question, when and why "[RequireComponent(typeof(Graphic))]" is necessary ? It seems to work without it in my script.
Never it's necessary. It's just a way to ensure that you only add this component to one object that has a Graphic attached to it. The script would work without it, but would crash if your object doesnt have a Graphic attached because GetComponent<Graphic>()
(line 10) would return null, line 20 would throw NullReferenceException
every frame.
BTW, if it worked for you, I would apreciate accepting the answer, so others can find the correct answer faster.
Answer by Magso · Jul 08, 2019 at 04:24 PM
Add the player's eulerangles.y
to it like this.
Vector3 angleDifference = transform.eulerAngles + player.eulerAngles;
if angleDifference.x > 90...
Also use GetComponent<SpriteRenderer>().enabled
because SetActive(false)
will deactivate the whole gameObject.
Thanks but it can't work as what we see depend of the field of view of the camera, not a constant "angle" variable. Is there a way to just make the back of a GameObject invisible ? With $$anonymous$$aterial or something else.
It wouldn't be a constant angle variable.
Vector3 angleDifference = transform.eulerAngles + player.eulerAngles;
can be used in update and referenced to within the function it's created in. You can use a 3D plane mesh which is only rendered on one side but it depends if you'd rather work with sprites only.
I don't have any "player" GameObject, it's just a 2D scene. Calculating exact angle between an object and the player in a 2D scene in perspective projection is a mess because of how field of view work. Try a script with perspective projection where a not centered rotating UI GameObject disappear when the player see it sideways (90°), with field of view = 120, and you'll see... :/
Could you explain how exactly i could convert an UI GameObject into something wich is rendered only on one side ? I'm not used to 3D and i don't know how to do it. Thanks
Your answer
Follow this Question
Related Questions
How I can do to make the objects do not take a lot of space in the game? 3 Answers
How to animate disappearing Object? 3 Answers
Zoom In: Reveal hidden object - help 3 Answers
Unhide unseen objects. 1 Answer
Show and Hide a prefab or GameObject 10 Answers