- Home /
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
and
sorry it took a few hours
This doesn't work?
animation[Reload].speed = Playspeed/4;
sadly i already tried this prior to asking but i have changed code since i tried
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
According to this it should work: http://docs.unity3d.com/ScriptReference/AnimationState-speed.html
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);
}
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
after making the changes something is still not quite right
im still getting the errors i mentioned
on lines
17,18,38 and 52
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.
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
Your answer
Follow this Question
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