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
Related
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!
In Unity 2D, is there a way to get the current sprite used either by a SpriteRenderer or an Animation? The idea I have is to have multiple GameObjects to use for a player character, and from the main one which receives animations and the script, edit the others based on the sprite currently used.
Now, I know that to get the current sprite name I can do this:
GetComponent<SpriteRenderer>().sprite.name
But that will only return the sprite used for the main GameObject's SpriteRenderer, and the name of it would not change even as animation changes it in Game Mode
GetComponent<SpriteRenderer>().sprite.name
This 100% does work , just tried it now and it is changing the name for each current sprite that the animator is changing.
You need to give more information and maybe a video since there is a problem somewhere else.
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.
I'm currently working on a 2D pixel Jump'n'Run. I want the player to be able to "buy" new skins for the player-character. I have multiple sprite-sheets. They all have the same structure. I'm using sprite animations.
How can I change the sprite-sheet at runtime? I found the following solution, but it's very resource intense: https://youtu.be/HM17mAmLd7k?t=1818
Sincerly,
Julian
The reason it's so resource intensive in the video is because the all the sprites are loaded in each LateUpdate(), which is once per frame. The script looks like it's grabbing all the sprites in the sprite-sheet and loading them every frame so that if the spriteSheetName ever changes, it will update the renderer on the next frame.
I don't believe that's necessary and in the video he mentions that it's just being used as an example. What I'd do is move it out of the LateUpdate() method and into its own method that can be called only whenever the user wants to change the sprite-sheet. So instead of mindlessly loading the sprites from the sprite-sheet each frame, you'll only load them whenever the user selects it.
That should accomplish drastically cutting down the intensity of this script because you're not loading all the sprites in a sprite-sheet and looping through each of their renderers on every single frame.
For the past 2 hours I have been searching everywhere and I can't find any resources to help me. The documentation is confusing (Especially for Animation) and every information I find is for a previous version of Unity that had a different animation system. Anyway, I have a GameObject with a Animator component attached. The Animator Controller has 4 animation states that it uses. I have a script attached to my GameObject and I can'd find out how to do the following:
1) Get the length any specific clip
2) Change the speed of any specific clip.
animator.speed = (some number) does not work because it changed the speed of ALL the clips. I want to change the speed of specific clips during runtime. Is there a way I can search the animation clip by name and change the clip speed? Or is there a way I can make a public AnimationClip, drag and drop the clip, and then edit the speed of the clip? And I want to change all of this during runtime, I know I can click on the Animation Clip and change the speed that way. Also, as I listed earlier, I need to get the length of the animation clip. I assume if I can change the speed of an animation clip, I can get the length of the clip because I'd have the reference to it. Any Help?
Use:
AnimationUtility.GetAnimationClips(GameObject)
By using this you will get all animationsClips in array from current gameObject. Then you will have everything you want.
See docs:
AnimationUtility.GetAnimationClips
AnimationClip