- Home /
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. This 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 which 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"); }
}
Answer by ExTheSea · Apr 25, 2012 at 06:45 PM
Hey
You just need one animation. The one to aim downsight.
This 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");
}
This 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 which is the first parent of the model. This way it won't interfere with for example shot or walk animations.
Hope this helps.
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?
Input.Get$$anonymous$$ouseButtonDown(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.
It works now I have 1 $$anonymous$$or bug when I start the game it starts the gun as already aimed in what should I do?
Your $$anonymous$$or 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.Get$$anonymous$$ouseButtonUp(1)).
Hope everything works now.
I had the animation set to play automatically it works perfectly now thank you for your help.
Your answer
Follow this Question
Related Questions
Reloading Help 2 Answers
Third Person Help 4 Answers
Animation Scripting Help 2 Answers
Stop animation after played 30 times? 0 Answers
Character controller problem? 2 Answers