• 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 kez717 · Jan 16, 2010 at 11:24 AM · animationcharacter

effective way to toggle character animations

im trying to get my character to be able to sit down on a key command and on a repeat of that key or on hitting any other movement key have them stand back up. i have 3 animations, 1 sittind down, 1 to be looped for sitting, and 1 for getting back up. even something like WoW would be nice where its animated properly until the character is seated then if the sit key is hit again they stand up or if they are moved they immediatly do whatever action was hit. so basically a script that would, on command, play the sit down animation once then loop the sitting animation and then would either play the stand up animation if the sit key if pressed again or move the character if a different key is hit and go back to the normal standing idle when its done.

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
1
Best Answer

Answer by Jaap Kreijkamp · Jan 18, 2010 at 12:10 AM

A lot depends on your character controller script but to give a starter:

var SitAnimationName : String = "SitDown"; var SittingAnimationName : String = "SittingDown";

private var inTransition : boolean = false; private var isSitting : boolean = false;

function COSitDown() { inTransition = true; // to block all actions until player really sits isSitting = true; // get the animation state of the sit transition animation var state : AnimationState = animation[SitAnimationName];

 // clamp the animation so we don't get weird artifacts
 state.wrapMode = WrapMode.ClampForever;
 state.speed = 1.0; // forward
 // play the animation
 animation.Play(SitAnimationName);
 // wait for the duration of the animation
 yield new WaitForSeconds(state.length);
 // play the sitting animation.
 animation.Play(SittingAnimationName);
 inTransition = false; // unblock

}

function COStandUp() { inTransition = true; // again block all actions var state : AnimationState = animation[SitAnimationName]; state.wrapMode = WrapMode.ClampForever; // gonna play animation in reverse state.speed = -1.0; state.time = state.length; animation.Play(SitAnimationName); // wait for duration of animation again yield new WaitForSeconds(state.length); state.speed = 1.0; // just for sure, reset play direction to forward isSitting = false; // we ain't sitting anymore inTransition = false; // unblock }

function Update () { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical");

 if (isSitting) {
     // we are sitting, see if we're trying to stand up
     // if in transtion don't do anything!
     if ((vertical != 0) && (!inTransition)) {
         StartCoroutine(COStandUp());
     }
     return; // don't move while sitting down!
 }
 if (Input.GetButtonDown("Jump")) {
     // we're gonna sit...
     StartCoroutine(COSitDown());
     return;
 }
 // do the actual movement after this comment

}

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 kez717 · Jan 18, 2010 at 01:44 AM 0
Share

works great except for 1 thing, when the animation to sit down or get up plays and while sitting the character is still able to get moved. is there any way to easily freeze the characters movement and rotation durring that time and unfreeze it on standing while having the movement and jump keys make the character stand if hit while sitting?

avatar image Jaap Kreijkamp · Jan 19, 2010 at 11:59 PM 0
Share

if you have your movement code after the last comment the player shouldn't be able to move while sitting.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

how to make your own character. 2 Answers

FPSInputController and Character Animation 1 Answer

Character animation problem 1 Answer

Characters Interaction with Mecanim Animations 2 Answers

Root motion / How to bake into pose Root Transform Position (x) 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