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 Melontank · Dec 01, 2013 at 08:25 PM · javascriptanimatorbooleanparameters

How can I use 1 key instead of two for toggling animation ?

Hi,

I have made animations for a flash light that can be holstered and un-holstered, using keys "f" and "g". It works fine, but I want to make the toggling on one key, "f". So when the player presses "f", it sets the bools in the "code" to their opposite. E.g if IsOn is false, it gets set to true upon the key being pressed.

The problem is, the bools are parameters in the mechanim animator and simply going like this "Bool = !Bool;" will not work because I get an error saying "Cannot convert boolean to unityengine.animator".

I am really stuck here and any help will be appreciated!

 function Update () {
 
 var IsOn : Animator = GetComponent(Animator);
 var IsOff : Animator = GetComponent(Animator);
 
 if(Input.GetKeyDown("f")){
 
 
 IsOff.SetBool("IsOff",true);
 
 IsOn.SetBool("IsOn",false);
 
 }
 
 if(Input.GetKeyDown("g")){
 
 IsOff.SetBool("IsOff",false);
 
 IsOn.SetBool("IsOn",true);
 
 
 }
 
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
0
Best Answer

Answer by tanoshimi · Dec 01, 2013 at 08:50 PM

You've got several issues here:

  1. Firstly, don't use GetComponent in an Update() loop. Get a reference to the Animator once in Start() and then reuse it.

  2. In fact, you're getting two references to the same animator component - IsOn and IsOff both point to the same thing - this is definitely unnecessary.

  3. By its very nature, a Boolean variable can only have two states. Therefore you don't need two Boolean variables to record both IsOn and IsOff - if IsOn is true then IsOff must be false (and vice-versa), so you only need a single variable, IsOn.

  4. You can use GetBool to get the current status of the variable.

Try this instead:

 #pragma strict
 
 var anim : Animator;
 
 function Start () {
   anim = GetComponent(Animator);
 }
 
 function Update () {
 
     if(Input.GetKeyDown("f")){
         var isOn = anim.GetBool("IsOn");
         anim.SetBool("IsOn",!isOn);
     }
 }
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 Melontank · Dec 01, 2013 at 09:03 PM 0
Share

Thank you so much ! Worked Perfectly ! There needs to be more tutorials with animation regarding scripting !

avatar image
0

Answer by markmmiller · Dec 01, 2013 at 08:53 PM

Hey there, I haven't used much of the mecanim stuff but the following code compiles and might solve your problem.

 var IsOn : Animator = GetComponent(Animator);
 var IsOff : Animator = GetComponent(Animator);
 
 if(Input.GetKeyDown("f"))
 {
     IsOff.SetBool("IsOff", !(IsOff.GetBool()));
     IsOn.SetBool("IsOn",!(IsOn.GetBool()));
 }
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

18 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

Related Questions

Can't Set Animator Boolean Parameter C# 1 Answer

Animator parameter stuck as True 4 Answers

How to set an Animator Boolean with JavaScript 1 Answer

Play animation while button is pressed fails when using boolean parameters. 1 Answer

Playing sprint animation with vertical axis? 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