Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Askiparait · Jul 08, 2019 at 11:44 AM · gameobjectvisibility

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 }
 
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Askiparait · Jul 09, 2019 at 04:40 PM 0
Share

It works great, thanks.

Just a question, when and why "[RequireComponent(typeof(Graphic))]" is necessary ? It seems to work without it in my script.

avatar image bpaynom Askiparait · Jul 09, 2019 at 06:08 PM 0
Share

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.

avatar image bpaynom Askiparait · Jul 09, 2019 at 06:11 PM 0
Share

BTW, if it worked for you, I would apreciate accepting the answer, so others can find the correct answer faster.

avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Askiparait · Jul 08, 2019 at 04:45 PM 0
Share

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.

avatar image Magso Askiparait · Jul 08, 2019 at 08:51 PM 0
Share

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.

avatar image Askiparait Magso · Jul 09, 2019 at 09:01 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

162 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges