how to reference an "armature action" of a bow using code? - unity3d

I'm trying to make a bow and arrow game. I downloaded a bow that has an animation(?) on it where the string is pulled back.
See:
I want to make a script that triggers that animation (shown on bottom-right of my gif) when Player left clicks. But I don't know how to reference the animation.
Is it something like _anim = GetComponent<Animator>(); and then animation.Play? I cannot tell what the name of the animation even is to do this.
Further.. it would be awesome to be able to control the animation's length depending on how long the user has held down the button, rather than playing it in full even if user only taps left click. I'm not sure how that would be achieved?

Ok, the question seems short but the solution will be long and you need to do a bit of learning. So, let's go through your question step by step.
I downloaded a bow that has an animation(?)
Yes, it is a 3D bow model with an animation in it.
I want to make a script that triggers that animation (shown on
bottom-right of my gif) when Player left clicks. But I don't know
how to reference the animation.
In order to trigger that animation, you need an Animator Controller attached to the bow model instance. Then you can click that Animator Controller to open its panel. After opening the panel, you can simply drag and drop your animation there. When you first drop your animation, it will be the default animation state. However, if you don't want to trigger the animation immediately, I would suggest you to create an empty state and make a transition to the bow animation.
Is it something like _anim = GetComponent(); and then
animation.Play?
More or less like this, but you should check the documentation for better understanding, this documentation explains it well and has a really good example for your use case.
I cannot tell what the name of the animation even is to do this.
Once you drag and drop the animation to the panel, you will see the name of the animation to use in the script.
Further.. it would be awesome to be able to control the animation's
length depending on how long the user has held down the button,
rather than playing it in full even if user only taps left click.
I'm not sure how that would be achieved?
There are many ways to play with animations, for example, you can set an animation parameter to stay in the animation state. Furthermore, you can also disable the Has Exit Time value from the transition to make it stop the animation immediately after the user stops holding the button.
Overall, when it comes to animations, Unity is quite powerful and my suggestion would be read the documentation and watch a couple of tutorials.
Hope this helps.

Related

how can I implement a Unity fps sprinting animation?

I'm currently working on an FPS game and I have these hand models as well as weapons. But I want it so that if I press the W key (move forward) it plays the sprinting animation with the arms. I initially thought of coding it like, if(Input.getkeydown(keycode.w))animation.play or something like that. But I'm using the character controller which doesn't use that. It uses the Input.getaxisraw. How should I implement this with the character controller?
Thank you a ton in advance
You will need to implement a basic animator controller. You can define all your states (Idle, Crouch, Jump, Sprint, Aim) and define the transitions between them. You may then add parameters like floats, triggers and booleans as conditions to transition between these states.
In the above example, if you want to move to a crouch state, you would use
GetComponent<Animator>().SetBool("Crouch", true);
Or for another example, if you want to move from Run to Walk. The transition (The blue lines indicating the movement from one state to another) will have a condition to move from walk to run and back based on the float parameter "Speed". if Speed>x, move from walk to run. else move from run to walk. In this case you would set
GetComponent<Animator>().SetFloat("Speed", floatValueHere)
and the controller will take care of the animation transitions.
This is just a pointing to the right direction. you will need to watch a couple of videos on animator controllers Check Unity's simple 8 minute explanation here Hope this helps!

How to add key frames to a new animation file in Unity?

I'm trying to script an sprite animation event for my player's character shooting animation, where the first frame of the sequence is the only one shown when no bullet is shot, and when you shoot it plays the rest of the frames.
I can see the animator window for the controller has an option to create new states and connect between them. The "Motion" row of the states accepts animation files (clips?), so I tried creating new Animation to put the shooting frames in, but it has no sprite properties in it as the automatically created Animation file (when importing the PNG sequence) has, I can't add anything and it doesn't allow for new keyframes to be created. So how do I actually use that to create an animation sequence for the animation state?
Thanks in advance.
I feel like it would be best if you rephrased / elaborated a bit more in your question. So i'm going to answer it the best I can. If you want to do it by script, you can try this answer:
https://answers.unity.com/questions/1031965/how-to-create-animation-event-at-specific-keyframe.html
If you want to add an event clip in the animation itself, try this:
https://www.youtube.com/watch?v=3_ZFSZr-sJI

Drop In Drop Out System with Input?

I'm trying to code a drop in drop out system with input. Basically i have 2 sprites and im trying to swap between controlling each one. Right now I have a button i press and it switches which sprite is active however it does not let me show the animations for the other sprite and i can't control it. It also makes the other sprite that i'm not swapped to go off screen. I need both sprites to stay on screen and be able to press a button and and be able to control the other with the same input. If anyone could help me it would be much appreciated i couldn't find anything about it on google considering this is a very specific problem.

It's there any easy way to implement GUI animation with unity GUI?

I used NGUI (free edition) in unity 4.x for GUI design. Now unity has its own GUI in 5.x. Is there any easy way to implement GUI animation with unity GUI?
For example, in NGUI,
I can easily move, rotate or scale a GUI element or make some fade-in and fade-out effects.
I can trigger the animation with buttons or hotkeys.
I can easily set the function to run when the animation finish.
How can I implement these with unity GUI?
You can animate GUI objects like every other object in Unity. Just attach an Animator and a Controller, add a parameter, and make an animation in the Animation window in unity. On button press can be done in a script, if you want keyboard input. Or with a function call - to a script - if you mean a GUI button.
Animation window can be found in Windows -> Animation
Ill add an example if needed.
An example of the controller:
Keypoints: The Trigger parameter, as the condition in the transistion into the animation state.
This makes it possible to add this line into the code:
anim.SetTrigger("Fire");
To play the animation.
Animation window:
Last but not least, ill add a video guide: https://www.youtube.com/watch?v=JeZkctmoBPw If that one wasnt good enough, one can always click around.
Unity GUI works in a different way to most GUIs. Instead of setting up a button which then remains there until ether you tear it down or user interacts with it, on every frame it asks you to draw the GUI objects.
So animation is inherent. If you want a button to flash, simply draw it in a different colour every ten frames or so. If you want it to move, update it's x, y co-ordinates.

Animation Synching in hand to hand fight in Unity 3d

As title suggests I am working on hand to hand fighting system. Now its just a start and I am also not professional so I have setup moves of players and enemy.
Now problem occurs when player hits enemy, it should trigger enemy "taking hit" animation. Now it triggers but both animation of "hitting" and "taking hit" at same time,so it doesn't look at it is meant to be.
So please anyone can set me on right path of what to do for like starting of "taking hit" when "hitting" animation reaches at some key frame or any other way.
I'll post code if you require but only a suggestion or link to some documentation will do.
Thanks
Probably the code is not the problem here. You should detect when the other player hits you, then set the parameter in the animator like this:
yourAnimator.SetBool("TakingHit", true);
Then in your animator, you should put the TakingHit animator and configurate the transition when the TakingHit parameter is activated.
Another thing you should considerate is that if you activate the "TakingHit" parameter bool in a loop, it will start playing the animation in a endless mode.