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 10chaos1 · Oct 15, 2014 at 12:17 PM · c#animationslowdown

how would i slow this animation

hello again ive done a rewrite of my previous weapon script so i could think it through and ive run into a snag, i want to decrease the speed of my reload animation if the player doesent press r before running out of bullets in the guns mag but the snag is that im new to coding, been doing it for two slow months learned a lot

so heres the code it mostly works but a s i said i want to slow the reload animation

 using UnityEngine;
 using System.Collections;
 
 public class Weaponcontrol : MonoBehaviour {
 
 public bool Hasammo = !false;
 public int Maxrounds = 17;
 public int Currounds = 17;
 public GameObject Gun;
 public float Playspeed = 1.0f;
 public Animation Fire;
 public Animation Reload;
 
     void Start () { 
     
         Fire =  Gun.animation.GetComponent (Fire.animation);
         Reload =  Gun.animation.GetComponent (Reload.animation);
     }
     
     
 
     void Update (){  
     
             if (Input.GetMouseButtonDown(0));
                 {   
                 Hasammo = !false;
                 }
                 
                 Gun.animation.Play (Fire);
                 Currounds-=1;
                 
                     if (Hasammo = false);
                     {
                     
                     Gun.animation.Play (Reload);
                     }
         animation[Reload].speed = Playspeed   //i want to take a a quater off playspeed and set the remainder to the animation speed as a float
                     
                     Currounds = Maxrounds;
                     
                     
         if (Currounds <= 0){
         
             Hasammo = false;
             
             }   
             else
             {  
             
             Hasammo = !false;
             
             if(Input.GetKeyDown(KeyCode.R)){
             Gun.animation.Play(Reload);
               Currounds = Maxrounds;
             }
         }
     }
 }

its changed a lot from my last question whats the bet way to do this

as requested here are the pics alt text

and alt text

sorry it took a few hours

unity 1.png (65.4 kB)
unity 2.png (341.8 kB)
Comment
Add comment · Show 7
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 Tanshaydar · Oct 15, 2014 at 12:38 PM 0
Share

This doesn't work?

 animation[Reload].speed = Playspeed/4;
avatar image 10chaos1 · Oct 15, 2014 at 12:41 PM 0
Share

sadly i already tried this prior to asking but i have changed code since i tried

avatar image 10chaos1 · Oct 15, 2014 at 12:45 PM 0
Share

just confirmed it it doent seem to work

avatar image 10chaos1 · Oct 15, 2014 at 12:49 PM 0
Share

and i cant just make the playspeed float of 0.75f as that give me the following errors

Assets/scripts/weaponcontrol.cs(35,55): error CS1502: The best overloaded method match for UnityEngine.Animation.Play(UnityEngine.Play$$anonymous$$ode)' has some invalid arguments and Assets/scripts/weaponcontrol.cs(35,55): error CS1503: Argument #1' cannot convert UnityEngine.Animation' expression to type UnityEngine.Play$$anonymous$$ode'

on any line that contains

Gun.animation.Play (Reload);

this would have been easy if that worked

avatar image Tanshaydar · Oct 15, 2014 at 01:29 PM 0
Share

According to this it should work: http://docs.unity3d.com/ScriptReference/AnimationState-speed.html

Show more comments

1 Reply

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

Answer by jokim · Oct 15, 2014 at 01:45 PM

The problem seems to be that you're never reinitializing the animation speed.

  animation[Reload].speed = Playspeed*0.75f; 

that line is fine, it does what you want. -EDIT Wrong, I'm sorry, I should have read the whole code above. a string should be inside the brackets, I assumed Reload was a string

But you need to set it back to normal speed when it's reloading the "other" way.

An easy way to do this without having to keep track of what the speed is at, is actually having 2 speed variables.

 float SlowReloadSpeed = 0.75f;
 float NormalReloadSpeed = 1.0f;

So, In one case, you're changing the speed to the slow one, then firing the animation. In the other, you change it to the normal speed, then fire the animation.

Oh, you might want to change the speed BEFORE launching the animation.


Edit -

I'm sorry, I missed a lot of errors in the above code. You seem to be confusing Animation with AnimationClips

Basically, An animation is a collection of AnimationClips.

I'm not sure how your animations are setup... Are they 2 clips inside the same animation ? or 2 animations with a single clip in them ?

In any case, you need the clip's name to play a specific animation, or change some of their parameters (such as the speed)

If an animation contains a single clip, then you can just use it like so :

 MyAnim.Play();

but if it contains more than one, I don't know exactly what would happen with the above call, But you can get it to play a specific animationClip if you specify a name in the call.

 MyAnim.Play("reload");

Now, when setting speed, You will need the animation clip's name. And make the call like so :

 MyAnim["clipName"].speed = 0.75f;



Edit 2-

     public bool hasAmmo = true;
     public int maxRounds = 17;
     public int curRounds = 17;
     public GameObject gun;
     public float slowReloadSpeed = 0.75f;
     public float normalReloadSpeed = 1.0f;
     public string fireAnim;
     public string reloadAnim;
     
     void Update()
     {  
         if (!gun.animation.IsPlaying(reloadAnim) && !gun.animation.IsPlaying(fireAnim))
         {
             if (Input.GetMouseButtonDown(0))
             {
                 if (hasAmmo)
                 {
                     gun.animation.Play(fireAnim);
                     curRounds -= 1;
                 }
                 else
                 {
                     animation[reloadAnim].speed = slowReloadSpeed;
                     gun.animation.Play (reloadAnim);
                     curRounds = maxRounds;
                 }
             }
         
             if(Input.GetKeyDown(KeyCode.R))
             {
                 animation[reloadAnim].speed = normalReloadSpeed;
                 gun.animation.Play(reloadAnim);
                 curRounds = maxRounds;
             }
         }
         hasAmmo = (curRounds > 0);
     }



Comment
Add comment · Show 19 · 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 10chaos1 · Oct 15, 2014 at 01:59 PM 0
Share

that seams to be what im looking for but i still have

Assets/scripts/weaponcontrol.cs: error CS1502: The best overloaded method match for UnityEngine.Animation.Play(UnityEngine.Play$$anonymous$$ode)' has some invalid arguments and Assets/scripts/weaponcontrol.cs: error CS1503: Argument #1' cannot convert UnityEngine.Animation' expression to type UnityEngine.Play$$anonymous$$ode'

on any line that contains

Gun.animation.Play

or

animation[reload].speed

so lines 17,18,30,36,52 and 53

is there a workaround to those errors

avatar image jokim · Oct 15, 2014 at 02:21 PM 0
Share

I will update my answer, I missed a lot of key points.

avatar image 10chaos1 · Oct 15, 2014 at 02:32 PM 0
Share

after making the changes something is still not quite right

im still getting the errors i mentioned

on lines

17,18,38 and 52

avatar image jokim · Oct 15, 2014 at 02:46 PM 0
Share

Updated my answer, It would help to know how your animations are set up. I could be more precise and give you more detailed info.

avatar image 10chaos1 · Oct 15, 2014 at 03:25 PM 0
Share

the animations were made in blender on the same timeline and split into the fire and reload animations on import of the guns model so i made the default take into fire and reload, fire starts at 0 and ends at 30 frames and reload starts at 40 and ends at 110 frames i hope thats what you meant

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Slow Down all Gameobjects Within another Gameobject's Collider 2 Answers

Idle Animations, Player Walking 0 Answers

Hi, Im trying to make a sword swing animation, but it is playing non stop how can I make it go back to Idle Animation? 4 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