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 LightMiner · Feb 02, 2015 at 11:00 AM · buttonaimpressshot

Press button to play "Aim" animation without repeating.

Hi everyone. I have a problem. I made a script for my character to play an "aiming" animation when I press "LeftShift" , and when I let go, it goes back to "Idle" animation, which is perfectly great. The problem is, when I press "LeftShift" button to aim, the animation keeps looping.. I want it to stop looping but stays steady till I release the "leftShift button. Is it possible?

Here is my script.

alt text

Also I have another problem. I have another animation for shooting. I can't really find a way to connect it with the script. Like when I click the"LeftShitf" button and "F" button, it shoots. Can someone please edit my script or give me an idea what should I do ?

NOTE: I'm using the animator so all my animations are in the animator.

animscript.png (6.0 kB)
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 Mr. Mud · Feb 02, 2015 at 12:13 PM

The solution to the looping animation is by turning off the toggle "Loop Time" in the animation which starts aiming. This way, once it reaches the end of the animation, the object will remain in whatever pose it was at that time; until another animation is started of course. You could also opt to create another animation which's keeps aiming, where the transition is "Exit Time" with a value of 0.9 or something like that; that way you can make the object seem more lively, as it still moves.

For your issue with shooting, add another parameter to your AnimationController (my advice would be a trigger). Only when your character is aiming, you can enter that animation. Here is an example of how your animator could look. The horizontal arrows use the transition "Exit Time". You could (depending on the animation) assign the same animation to "Release Aim" as you would to "Take Aim", but with a negative playback Speed.

alt text


 //Get a reference once; more efficient.
 private Animator anim = GetComponent<Animtor>();
 
 [SerializeField]//Assignable from inspector.
 private KeyCode //Make the controls more dynamic.
     aimKey, 
     fireKey;
 
 private void Update()
 {
     if (Input.GetKeyDown(aimKey))
     {
         anim.SetBool("WeaponRaise", true);
     }
     if (Input.GetKeyUp(aimKey))
     {
         anim.SetBool("WeaponRaise", false);
     }
 
     if (Input.GetKeyDown(fireKey))
     {
         anim.SetTrigger("Shoot");
         //Do anything else related to shooting.
     }
 }



Also why are you using an image of script, rather than copy-pasting it? I hope this helps you solve your trouble.


animator.png (35.3 kB)
Comment
Add comment · Show 3 · 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 LightMiner · Feb 03, 2015 at 04:16 AM 0
Share

I think I almost got it, but there is an error I don't know how to fix it. it says "(9,25): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent(System.Type)'"

In this line :

//Get a reference once; more efficient. private Animator anim = GetComponent();

avatar image Mr. Mud · Feb 03, 2015 at 06:53 PM 0
Share

$$anonymous$$ight be one of two things: I didn't test this code, so I skipped that issue. It seems that you cannot acces instance methods to set default values. Quick fix: move it to "Awake()" or "Start()". Another slight issue, it must read Animator; not "Animtor" (without the second 'a').

Or you did not include "Animator" surroundend arrows.

Also note that this is a C# script, so it might need to be rewritten a bit should you use javascript (or boo script for that matter). Something I cannot quite figure out from your sample.

Here is a revised part of the script.


 //Get a reference once; more efficient.
 private Animator anim = null;
 
 private void Awake()
 {
     anim = GetComponent<Animator>();
 }


avatar image LightMiner · Feb 04, 2015 at 05:14 PM 0
Share

Thanks man! totally worked! :)

avatar image
1

Answer by SnotE101 · Feb 03, 2015 at 05:08 AM

Try:

bool aim = Input.GetKey(KeyCode.LeftShift);

 anim.SetBool("WeaponRaise",aim);
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Increment a loop iteration on button press? 4 Answers

Trying to move object consantly after button is pressed 1 Answer

player shoot at cursor/normalize problems 2D,normalize and aim 2D 0 Answers

How To play sound with a keypress 2 Answers

On button press/hold = input from a keyboard? 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