Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 CrimsonChin · Feb 25, 2015 at 12:19 PM · 2d4.6

Unity 4.6 Stop Canvas from flipping

I'm trying to create a flexible little speach bubble attached to my player. To do this I have added a canvas to my player entity with UI Text on it. The canvas is then set to "World Space" so that it follows the player along as he moves. This works fine:

alt text

To make the player walk the other way I use a Flip() method which flips the transforms localScale like this

 void Flip()
 {
     faceright = !faceright;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }

Problem with this is it flips my canvas and I end up with this:

alt text

How do I stop this from happening? I have tried all sorts of things and none seem to give me the correct solution. I have tried reverting the child transform locale scale by adding something like this to the Flip method

     foreach (Transform child in transform)
     {
         Vector3 childScale = child.localScale;
         childScale.x = Mathf.Abs(childScale.x);

         child.localScale = childScale;
     }

And I also tried adding a billboard script to the Canvas to make it look at the camera each update:

 public class Billboard : MonoBehaviour 
 {
     Camera _camera;
     
     void Start() {
         _camera = Camera.main;
     }
     
     void Update()
     {
         transform.LookAt(transform.position + _camera.transform.rotation * Vector3.forward,
                          _camera.transform.rotation * Vector3.up);
     }
 }

I think my anchors etc are all wrong in the canvas but I'm totally out of my depth. Anybody know what I'm doing wrong/not doing?

normal.png (2.3 kB)
flip.png (2.3 kB)
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 fafase · Feb 25, 2015 at 12:21 PM

You are flipping the parent object so anything below the hierarchy will flip with it.

Make your top object a controller object with the text mesh as one child and the sprite as another child.

Then you can independently flip things:

 [SerializeField] private Transform playerTr = null; // Drag sprite object in there
 
  void Flip()
  {
      faceright = !faceright;
      Vector3 theScale = playerTr.localScale;
      theScale.x *= -1;
      playerTr.localScale = theScale;
  }
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 CrimsonChin · Feb 25, 2015 at 12:26 PM 0
Share

Ok, I take it I would need to move all my physics etc to the parent object too so that that everything moves together? Thats a pretty good idea!

avatar image fafase · Feb 25, 2015 at 12:49 PM 0
Share

Well that is how I tend to organize my coding, top object holds the controllers of the objects, sub objects will be either controlled by the top controller or some scripts that are specific to that object at the level.

In your case, I would have an Input controller that passes the data to some controller at the parent level and pass down the info to the appropriate child if necessary.

That also means your flip method is on a script on the child object and that method is called from above either via a direct reference (not so happy but well) or via an event (real happy yeah).

avatar image Stoyanow · Jun 10, 2015 at 03:32 PM 0
Share

I followed this instructions, and now my character doesn't have animations (yes i transfered the animator to the parent object) and it can jump infinitly

avatar image
0

Answer by aadyainfotech · Dec 23, 2015 at 04:32 PM

@CrimsonChin: You can keep your setup as it is. Just add a transformation to your canvas in your script same as the players transformation:

Also remember to flip back the canvas along with your player.

 public GameObject canvasBoard; // variable
 
 
 transform.localScale = theScale;
 canvasBoard.transform.localScale = new Vector3(-1,1,1) // add this after your player transform.

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity 2D 4.6b UI: Why are instantiated prefab sprites rendering over UI elements? 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Issues with GameObjects and GUI 2 Answers

Spawn random enemies in 2D 1 Answer


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