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
2
Question by puzzledragon · Sep 11, 2011 at 03:13 PM · swordgetbuttondown

play animation on button press

Hello Im trying to set my game up so that you can slash your sword by pressing a button, I have made an animation for this and written a code but when I press the button the animation won't play this is my code.

 if(Input.GetButtonDown("]")){
 animation.Play("slash");

}

i already set this button up in input what should I do.

Comment
Add comment · Show 5
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 LegionIsTaken · Sep 11, 2011 at 06:58 PM 0
Share

I am not totally sure that you can use !"#¤%&/() and so forth using GetButtonDown unless you created them in the input manager. $$anonymous$$aybe use Get$$anonymous$$eyDown ins$$anonymous$$d.

Here is a list of all the keyCodes that Unity offers.

http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$eyCode.html

avatar image puzzledragon · Sep 12, 2011 at 10:45 PM 0
Share

thank you this works but now the animation plays repeatedly i want it to play once after you press the button

avatar image robert_mathew · Sep 13, 2011 at 05:52 AM 0
Share

can u specificity what you want when u press the button do u want to play animation one time or repeatedly when u press the button .what is happening now using this code

avatar image robert_mathew · Sep 13, 2011 at 05:54 AM 0
Share

you can break the Boolean loop or in fbx make animation once ins$$anonymous$$d of loop

avatar image puzzledragon · Sep 13, 2011 at 10:59 AM 0
Share

how do i break the boolean loop

7 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by robert_mathew · Sep 12, 2011 at 03:03 AM

      public bool animation_bool;

          void Update()
                     {

              if(animation_bool == true)
                     {
                 animation.Play("slash");

                     }


            if(Input.GetButtonDown("space"))
                     {
               animation_bool = true;

                     }
 
 
 
                }


above one is c# coding

     var  animation_bool : boolean ;

       function Update()
                      {

           if(animation_bool == true)
                      {
                  animation.Play("slash");

                      }


           if(Input.GetButtonDown("space"))
                      {
               animation_bool = true;

                      }
                 
              }

above one is java script you can use any one of these it is checked codes this solve your problem

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 Eli_is_567 · Jan 10, 2017 at 04:22 PM 0
Share

I have this

using UnityEngine; using System.Collections;

public class Putter : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () {
     GetComponent<ConstantForce>().enabled = false;
 }

 // Update is called once per frame
 public bool animation_bool;
 void Update () {
     if (Input.GetButtonDown("Jump"))
     {
         animation_bool = true;
         GetComponent<ConstantForce>().enabled = true;
     }
     if (animation_bool == true)
     {
         animation.Play("Putter");
     }
 
 

     void OnCollisionEnter(Collision other)
 {

     Destroy(gameObject);
 }
  

 }

}

i'm trying to make a $$anonymous$$i Golf game and I want it to play the animation then hit the ball and Destroy. but I dont know how to get it to play the animation first

avatar image
0

Answer by robert_mathew · Sep 11, 2011 at 04:08 PM

 public bool animation_bool;

          void Update()
                     {

               if(animation_bool == true)
                     {
                     animation.Play("slash");

                     }\\animation_bool ends


                if(Input.GetButtonDown("]"))
                     {
                   animation_bool = true;

                     }

if you try call from inside the gui button loop the call will be a fraction of seconds if you animation to play continuously when gui button is pressed you should call using Boolean function like i have done changes in your coding.

          }\\update ends
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 puzzledragon · Sep 11, 2011 at 05:35 PM 1
Share

this just gave me several compiler errors

avatar image
0

Answer by fjodorbelet · Dec 28, 2012 at 09:30 PM

my animaton play from itselves when i check the animation_bool box

and if i uncheck the box my animation doesn't work if i press the button

Please help ????? sorry for bad english :(

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 Seth-Bergman · Dec 28, 2012 at 10:35 PM 0
Share

just change this line:

 animation_bool = true;

to this:

 animation_bool != animation_bool;

:)

avatar image
0

Answer by Stiangul · Jul 03, 2014 at 07:24 PM

Just to fix your script a little, it wont work if you type if(Input.GetButtonDown("]")){ it has to be like this:

 function Update()
 {
     if(Input.GetKeyDown(Keycode.R))
     {
     //in here you can put whatever you want, for example: renderer.material.color = Color.red for example or as you want animation.play
     }
 
 }

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 ahsanbutt · Nov 21, 2017 at 01:36 PM

hi judges * by Time.delta *time animation_bool = true;

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
  • 1
  • 2
  • ›

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

15 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers

animated sword + script 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