• 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 /
  • Help Room /
avatar image
0
Question by EllisNguyen · Aug 27, 2015 at 01:36 PM · c#animationanimator

Pressed button to play animation?

Hey guys!! Im new to unity3d engine and im working on a FPS project. I have my gun model i i've animated it to "GunFire", "GunSprint", "GunAim"..... (Some more) My question is, how to make the gun that shoot, make damage and play my animations?

For example: My gun deal 30 damage, when i hold "LShift" and run around, it play "GunSprint" animation, and when i press and hold my "RMouse", it play "GunAim" animation?

Please help :< Thanks u guys so much (Im using FPS_Controller prefab from the Default Asset as my player)

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

Answer by pwego · Aug 27, 2015 at 01:59 PM

I'll try to answer some of it since Im just blankly staring at the screen after just waking up, so add an animation to your prefab as in just add component and type "animation" not animator. Click Animations in the inspector and then see where it says size? Type 1 in the size, then drag your animation into the element 0 AND into the regular animation spot above that, so twice.

Then in code under something like

mouse 0 is left click 1 is right click and 2 is middle mouse button

 if (Input.GetMouseButtonDown (1)) {
 
 gameObject.GetComponent<Animation>().Play ("animation exact name");
 
 }
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 EllisNguyen · Sep 04, 2015 at 03:31 AM 0
Share

Heyyy, what the different between "GetButton" and "GetButtonDown" @pwego

avatar image
0

Answer by EllisNguyen · Aug 27, 2015 at 02:19 PM

To play other animation, i just have to write the same as that one right? I've created a new c# script, and, this code is in the Void Start or Update?

Dont hate me because of asking too much, im new :< @pwego please help

Comment
Add comment · Show 10 · 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 EllisNguyen · Aug 27, 2015 at 02:27 PM 0
Share

I made some animations, put it in an animator with all the animations. Create "Aim", "Sprint" as Bool and "Fire" and "AimFire" as Trigger How can i put them on the code? plz I've add the animator to my gun. I add "GunNormal" to animation in my gun's animation (cuz i set it as default in my animator). Plz help @pwego :<

avatar image pwego · Aug 27, 2015 at 02:43 PM 1
Share

I use void Start for setting things at the beginning of running my code like what things equal. If i want code to run depending on what happens later in the game like buttons being pressed I would use Update. If things need to be really fast or faster than once a frame you can use FixedUpdate.

Im bad at explaining well but the method I posted is for 1 animation. If you want to add more animations for that one gameobject just change the size to 2 or 3 or however many animations. Then drag the animations into the elements and refer to them in code like:

 gameObject.GetComponent<Animation>().Play ("exact name");

Only do this if that didnt work cause i find this more tricky, but you could use arrays.

put at top above start

 public Animation[] animationClip; 

then on its own not in either start nor update type your own void like:

 void PlayAnimation (int clip){
         GetComponent<Animation>().clip = AnimationClip[clip];
         GetComponent<Animation>().Play();
 }

Then in whatever code you want to play an animation (attached to same game object) do something like:

         PlayAnimation(#);

Change # to whichever animation clip you want. So element 0 would be 1. element 1 would be 2.

avatar image pwego · Aug 27, 2015 at 03:06 PM 1
Share

Somewhat, but dont put it in the start method only put it in update. Also you have 2 animations for left click hold.

Get$$anonymous$$ouseButton is pressed once Get$$anonymous$$ouseButtonDown is it being held down.

avatar image pwego · Aug 27, 2015 at 03:14 PM 1
Share

O also, all this im telling you is for the "Animation" component you added to your gameobject, NOT the "Animator". Animator is the thing by itself with arrows pointing at different animations. This code is for doing its manually in your code.

Both work well but if you understand the animator well enough and you already made your parameters, floats, bools and whatnot. Then do:

above start:

 bool boolnameincode;
 float floatnameincode;
 private Animator anim;

in start:

anim = gameObject.GetComponent ();

in FixedUpdate or Update:

         anim.SetBool ("bool name in animator", boolnameincode);
 anim.SetFloat("float name in animator", floatnameincode);


avatar image pwego · Aug 27, 2015 at 04:02 PM 1
Share

Thats another question, if I answered your first question alright can you make it as the correct answer?

If you need more help you can post another question and I can help you.

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 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

Related Questions

Having problems with making a Navmesh Agent stop and play an attack animation 0 Answers

Unassigned Reference Exception 0 Answers

Animator transition not working on parameter 0 Answers

How to add programatically animationClips to one animation from Resources folder? 0 Answers

2D Animation disables Player Controller 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