Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mightybob · Mar 05, 2017 at 05:59 PM · c#animationanimatorframe

Play an animation from the animator at a specific FRAME using anim.Play();

I'm trying to make it so my 2D rigged character aims his gun towards the mouse. I have an override layer with an animation called "Aim", that only affects the arm. It has 180 frames, each frame corresponding to a rotation value. (e.g. frame 90 = 90 degree rotation of the arm)

I need to set the "Aim" animation to a specific frame so that it will rotate. So far all I've found that could be useful is anim.Play(). Example of what I got so far:

 anim.Play ("Aim", 1, 0.5f);

"Aim" being the animation name, of course. "1" is the layer, "0.5f" is the time in the animation for it to play. My problem here is that it goes from 0 - 1, not from 0 - max frames. So I don't know how to go to a specific frame using this function. I'm basically looking for a math equation that'd let me set the animation to a specific frame using the .Play() function. I know it's possible as I've done it before, but I don't have access to my old project files anymore. Can someone lend a hand? :/

Comment
Add comment · Show 2
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 eldruz · Mar 06, 2017 at 01:30 PM 0
Share

This looks similar to that question. Hope it helps.

avatar image mightybob eldruz · Mar 07, 2017 at 02:13 AM 0
Share

Thanks, but isn't that using the animation component, not the animator? (which is what I'm using)

3 Replies

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

Answer by mightybob · Mar 07, 2017 at 02:25 AM

Answering my own question in case someone else is wondering. This is what I've figured out:

 animator.Play ("AnimationName", 1, ( 1f / total_frames_in_animation ) * desired_frame);
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 NecrosDk · Mar 06, 2017 at 02:30 PM

I would look into making a generic animation for what is supposed to be displayed when your character aims at something, and then changing the transform of the gun part to look at your mouse, by scripting its rotation.

Something like this:

  void Update () {
          Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
          transform.rotation = Quaternion.LookRotation(Vector3.forward, mousePos - transform.position);
      }

Comment
Add comment · Show 2 · 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 mightybob · Mar 07, 2017 at 02:11 AM 0
Share

It's important that the rotation is done by animations so I can have it look natural by blending animations when the character is looking around with his head or ai$$anonymous$$g with his arm.

$$anonymous$$g. head is looking at mouse but still is blended with the running animation so it still bobbles and doesn't look un-animated.

avatar image NecrosDk · Mar 07, 2017 at 11:06 AM 0
Share

I don't know if it's sufficient for what you're trying to achieve, but let's say you want a character running, and while he runs, he bops the gun up and down. In that case, I would make a generic up and down bop animation on the y axis and rotate the gun physically, so that you only need one single animation. $$anonymous$$aybe I misunderstood your question..

avatar image
0

Answer by unity_RJs1M1mPBz0Pxg · Dec 31, 2021 at 02:17 PM

ayo whats the second parameter equal to 1 for ? My thing returns me a warning.

Comment
Add comment · Show 2 · 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 unity_RJs1M1mPBz0Pxg · Dec 31, 2021 at 02:19 PM 0
Share

Im mean what's the layer for I am so confused I didnt know layers applied to specific animation frames like that wth

avatar image FColor04 · Dec 31, 2021 at 06:32 PM 0
Share

Layers are used to blend animations between them, each layer has unique state, so one can be playing arms and hand animations and second can do legs for example, if you use -1 for layer, it means to look for the first animations that matches the provided name

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

335 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 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 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 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 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 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 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 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 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 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 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 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

Issue with mecanim playing an animation using setbool 1 Answer

Unity 5 Broken Animations? 3 Answers

Efficient way to instantiate a sprite that plays an animation then destroys itself? (3d space) 1 Answer

UI Text Alpha not fading out in animation 0 Answers

issue using OverlapCircle and AnyState in Unity Animator,issue using OverlapCirlce and AnyState in Unity Animator 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