• 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 devcor · Jul 03, 2014 at 11:41 PM · c#movementcharacterparentchildren

Children must not flip with parent.

Hello. I've got a 2d platformer, and I've made character controller by the tutorial from unity's "Live Training".

Now I've got some problems with it. The movement to the left there realised through flipping X scale of the character, thus making him go left.

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

But here's a problem. When character moves left, all of his child objects also change their x-scale to -2 (i've made them 2.0 obviuosly up from 1), which I don't want, I want to rotate only parent object, but not the child.

If you can, help me how to make specific child not to flip with parent. Or all of them. I've been googling for 4 hours now, please, help T_T

Comment
Add comment · Show 1
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 podmaster · Jul 04, 2014 at 03:33 AM 0
Share

Can explain why you need to keep the child element in scale, maybe there is a cleaner solution.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Jul 03, 2014 at 11:44 PM

Iterate through each child and swap them back, as follows

 foreach (Transform child in transform){
         Vector3 childScale = transform.localScale;
         childScale.x  *= -1;
         transform.localScale = theScale;
 }

 
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
avatar image
1

Answer by devcor · Jul 04, 2014 at 12:11 PM

I've made it more elegant way, without iterating through all of the children:

 private GameObject child;
 
 child = GameObject.Find ("SpriteRotate");
 
 Vector3 childScale = child.transform.localScale;
         childScale.x *= -1;
         child.transform.localScale = childScale;
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
avatar image
0

Answer by podmaster · Jul 04, 2014 at 01:02 PM

Find() its not recommended to use. You can create a new script "keepScale" attached to every child you want to keep in scale and do something like this:

 var originalScaleToKeep : float; 
    function Start(){
 originalScaleToKeep : float = transform.localScale.x;
     }
 function Update(){
     transform.localScale.x = originalScaleToKeep;
 }
Comment
Add comment · Show 2 · 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 Kiwasi · Jul 04, 2014 at 09:25 PM 0
Share

The downside to this is that it runs every update cycle, use with caution

avatar image Zoelovezle Kiwasi · Jan 22, 2016 at 10:21 AM 0
Share

Add if statement as ,

 if(transform.localScale.x <= 0 )
    transform.localScale.x = ScaleTo$$anonymous$$eep ;

You could even use $$anonymous$$athf.Abs(float transform.localScale) ;

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

I want to play an Audio Sound for a character while running. 0 Answers

My Character keeps moving even when i release the key(2D) 1 Answer

Distribute terrain in zones 3 Answers

[C#] Jump on slopes 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