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 sloththegarry · Dec 22, 2018 at 11:40 AM · unity 2dfliplocalscalestate machineflipping

Lock character's LocalScale during Idle animation

Hi, I would appreciate some suggestions for the following problem; I've got a character with two basic animations, Walk and Idle. During Walk animation you can see the side of the character, while during Idle animation the character is facing the camera. The flip method functions correctly while walking. However, it somewhat disfigures the character during idle animation when the scale is set to -1. I'd like to lock the character's x scale to +1 when he's idling. My solution was to add the following animation behavior to the Idle animation.

 public class KeepScaleBehaviour : StateMachineBehaviour {
 
     private Transform playerTransform;
     private GameObject player;
     float updatedXscale;
 
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         player = GameObject.FindWithTag("Player");
         playerTransform = player.GetComponent<Transform>();
 
         if (playerTransform.localScale.x > 0)
             return;
 
         else
             playerTransform.localScale = new Vector3(playerTransform.localScale.x * -1, playerTransform.localScale.y, playerTransform.localScale.z);
     }
 
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         if (Input.GetAxis("Horizontal") > 0)
             updatedXscale = 1;
         else
             updatedXscale = -1;
 
         playerTransform.localScale = new Vector3(updatedXscale, playerTransform.localScale.y, playerTransform.localScale.z);
     }
 }

This behavior script seems to be doing the job, but I have a feeling that there's a simpler or more efficient way of solving this issue. Feel free to share any ideas. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_oEoMzC5COK722Q · Dec 23, 2018 at 04:04 PM

I suggest you put in a check in the flip function of your character controller script—an if statement or a boolean—that checks whether your character's rigidbody.velocity is greater than zero, and if so then the flip function can be executed. I haven't tried it myself yet but I believe this is a viable option. The problem with your method is that firstly, it is costly to the performance if you have multiple character's with the same state machine behaviour (the calculation is made every-time character enters idle state) and secondly there's a lot of potential for unwanted behaviour, especially when you add more animation layers or clips.

Comment
Add comment · Show 1 · 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 sloththegarry · Dec 23, 2018 at 05:05 PM 0
Share

Thanks for the answer. I actually did run into problems with my solution later on. I tried what u suggested. However, as soon as the character starts moving to the left, the scale is set to -1 again. So even if my X velocity reaches 0, the scale is still at -1 when I let go of the movement button and the idle animation starts playing. I think I might be able to elaborate on your idea a little bit. I also tried to set the scale to +1 when the velocity reaches 0 but that only inverts the flip functionality :D

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

99 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

Related Questions

Changing transform.localScale of a prefab in the script not working. Help pls! 1 Answer

Having trouble with flipping my projectile. 0 Answers

LocalScale not working idealy 1 Answer

How do I flip an enemy that's automatically walking and targeting the player? 1 Answer

2D Input.GetAxis not recognized when local scale is flipped 0 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