• 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 Dreave · Apr 25, 2012 at 05:56 PM · animationaimdownsights

Aim down sights animation help

I have made an animation inside unity for my gun theres two animations 1 that aims in and one that aims out I have wrote a simple script that triggers them if the RMB has either been pressed down or up. I was hoping it would make my gun aim down its sights when the RMB has been clicked and when let go of plays an animation back to its starting position. T$$anonymous$$s does not work instead my gun sort of jerks into the aim down sights position and then changes back before I have let go of the RMB. How would I change my script (or animations) so that my gun plays an aim down sights animation and stops where it finishes before I let go of the RMB w$$anonymous$$ch plays the animation back to its original place?

Script

 function Start () {

animation.wrapMode = WrapMode.Once;

}

function Update () {

if(Input.GetButtonDown("Fire2")){ animation.Play("aim down sights"); }

if(Input.GetButtonUp("Fire2")){ animation.Play("aim down sights2"); }

}

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 ExTheSea · Apr 25, 2012 at 06:45 PM

Hey

You just need one animation. The one to aim downsight.

T$$anonymous$$s is a part of my aimAnimationScript for a M4:

 if(Input.GetMouseButtonDown(1))
 {
 animation["ScopeAnimationM4"].time =0;
 animation["ScopeAnimationM4"].speed = 1;
 animation.CrossFade("ScopeAnimationM4");
 }
 
 if(Input.GetMouseButtonUp(1))
 {
 animation["ScopeAnimationM4"].normalizedTime=1;
 animation["ScopeAnimationM4"].speed=-1;
 animation.CrossFade("ScopeAnimationM4");
 }


T$$anonymous$$s way you can just reverse the animation to aim downsight. By the Way: I did it so that the scope animation is not on the model but on an empty gameobject w$$anonymous$$ch is the first parent of the model. T$$anonymous$$s way it won't interfere with for example shot or walk animations.

Hope t$$anonymous$$s helps.

Comment
Add comment · Show 5 · 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 Dreave · Apr 25, 2012 at 08:14 PM 0
Share

Thanks for posting, I have changed the animation name to the name of my animation but it does not do anything, on the top and 8th line what does the 1 mean I am used to putting Fire1 or Fire2 can you help me out?

avatar image ExTheSea · Apr 25, 2012 at 08:26 PM 0
Share

Input.GetMouseButtonDown(1) is the same as your "Fire2" getmousebutton is just another way to access the mouse buttons; 0=leftbutton;1=rightbutton;3 is i think middle button.

Oh did you put the part i have posted in the update function? Sry i didn't mentionend that.

Hope it works now. If not just keep asking I will revisit this question from time to time.

avatar image Dreave · Apr 25, 2012 at 08:45 PM 0
Share

It works now I have 1 minor bug when I start the game it starts the gun as already aimed in what should I do?

avatar image ExTheSea · Apr 25, 2012 at 09:03 PM 0
Share

Your minor bug is not really common when i use this technic. You should check whether in any awake or start function the scopeanimation gets called or if your scopeAnimation gets played automatically. If not you could insert in your Start function:

animation["YourScopeAnimation"].normalizedTime=1;

animation["YourScopeAnimation"].speed=-1;

animation.CrossFade("YourScopeAnimation");

Btw: If you didn't already noticed that's the syntax in if(Input.GetMouseButtonUp(1)).

Hope everything works now.

avatar image Dreave · Apr 26, 2012 at 03:59 PM 0
Share

I had the animation set to play automatically it works perfectly now thank you for your help.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to simply play this animation... Help please. 1 Answer

Reacting to Mouse animation... 2 Answers

Loop animation on button hold 2 Answers

play animation sequence 3 Answers

How could it be done "Cooldown" for a shield ? 2 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