How to control Unity Animation? - c#

As far as I know, there are ways to manage Animation
Immediately,
Managing in the form of objects.
Managing with Sprite Images.
Is it effective to manage animations
in object format to manage character's joints in 2D animation?
What should I do to make it easier for me to understand Unity Animation?
As a beginner, we need a lot of data. I need your help. Help me.

I am going to explain the animation by manipulating GameObjects.
You need to add an Animator component to the GameObject you wish to animate. The animator component needs an animator controller. You also need to create an animation clip which represents an animation.(Animation controller is automatically created when you create an animation clip)
Now, to get started with animation you need to focus on animation clips. After you add an animation clip, you can record an animation into it. You do this by hitting the record button in the Animation window. While recording, any changes made to the GameObject will be recorded into the animation clip. (For example, you might move your GameObject). Any such change will create a key frame in the Animation timeline. The time point where key frame should be created can be changed.
Unity will interpolate the changes between two keyframes automatically.
However, there is also an animation curve which allows you to define how changes are applied between time points.
After you record animations you can define how transitions between different animations are made in the Animator Window.

unfortunably I am not really sure what your question is about?
For question this might be helpful:
https://www.youtube.com/watch?v=PuXUCX21jJU
Usually you have a Image File with the animation movements of your 2D Image object and using the "Sprite Editor" to cut them out for Unity.
You then add this Clip on a Animimation Component to be added to your "GameObject".
Since this is a "C#" question, maybe you want to know how to access this Compnent. The best is to use it in the "Init()" and add:
var anim = GetComponent();
Now you can use the "Animation" Component to play the configured Animation Clips.
I hope this helps you a little bit.

Related

Create dynamic climbing method

I would like to understand how to create a script to climb objects of different heights like in this video this.
I know you have to use LayerMask to understand if you are in front of an object, but I don't understand the script that brings the character from below to above the object or on the other side (if it is a wall to climb over) .
For a ladder, I thought I'd put "gravity = false" and "transform.up * Input vertical" to go up or down. But to position yourself above a wall in this way, what script do it's used?
And how does the same animation be usable on walls of different heights as in the video ?
Use an animator, setup your animations in your state machine, in the animator click ‘apply root motion’ and your animation will apply to your character position.

Stop animation looping back to initial position in Unity3D?

I've made an animation on an Image object in my Unity game. When the animation has finished it animates back to its original position! I'd like to stop this from happening. I've tried Googling and searching here on SO but can't see any solutions so far! My animated object has an Animation and Animator components attached to it. The Animation component has an array of 3 animations. I only want to ensure one of them does not loop back to its default/original position.....
Here is a screenshot of the animation components attached the gameobject:
I suppose in the animation sequence the last frame is the same as the first frame? This is what usually happens and restores the animation to the original position. Try removing the "loop" tick of the animation as well as check the first and last frame.
I had the same issue, and I solved it just now. You must unchecked to Root Transform Position(XZ) / Bake Into Pose, from inside your animation.

Changing Sprites using scripts in Unity2D C#

I am wanting to make a birds-eye view pixel-art game.
I currently have two sprite sheets set up, and split and whatnot
groundSheet and characterSheet these are split up into
ground_0_0_0 (A concrete floor)
ground_1_0_0 (grass)
character_0_0_0 (man idle animation frame 1)
character_0_0_1 (man idle animation frame 2)
character_0_1_0 (man run animation frame 1)
character_0_1_1 (man run animation frame 2)
character_1_0_0 (woman idle animation frame 1)
character_1_0_1 (woman idle animation frame 2)
character_1_1_0 (woman run animation frame 1)
character_1_1_1 (woman run animation frame 2)
The numbers after are a note as to:
first number - the main set of sprite animations (eg man)
second number - the animation set in use (eg run or idle)
third number - the frame of said animation.
(the ground has this as i plan to have animated grounds late on)
Now, I wish to make a script for the character (and ground alike) that has an editable value that is view able in the unity editor, for example how things like the sprite renderer has sprite, colour etc. Which dictates what first number to use (see above) what second number and the delay for the animation of the third number. This will then tell the sprite renderer what pictures to load and how quickly to load them. I also wish for the script to scan for the file starting with for example character_0_0_ and then count how many files after it, so it knows what to do when animating. I would like this script to be checking for a change in one of the variables viewable in the unity editor to change and as soon as it does it checks everything it needs for an animtion.
Something else could be done where there is only 1 box to edit in unity, which you put character_0_0_ or ground_1_0_ or something similar, and it checks everything that way (it also makes the script universal, and usable on the ground, character and walls (which I am adding later)).
This may seem confusing, but it make sense for me and many of you will probably mention a much easier way to do animations and such, but please only say these if it does what I want above.
For scripts and such my file layout:
/Assets
/scripts
ground.cs
character.cs
/sprites
characterSheet.png
character_0_0_0
character_0_0_1
character_0_1_0
character_0_1_1
character_1_0_0
character_1_0_1
character_1_1_0
character_1_1_1
groundSheet.png
ground_0_0_0
ground_1_0_0
(For some reason Stack overflow said the above was code, so i had to make it as that)
ground.cs and character.cs are the scripts in which I want to made as explained above.
In my object view thingy I have
Main Camera
ground
character
I am practically a newb to C# and JS I know bascially the grammar of C# (like where to use {} and put ; at the end of the lines). If you help me with this, i request that you explain the script, like use the // thing to simply explain what each command does, I know a few but not all of them. And I know someone is going to say it is really well documented in tutorial X and such, but most tutorials are not in unity 5 and while helping with the matter do not touch on it exactly.
Thank you for your help, if there is anything about this question/request that you do not understand (It is pretty complex) I will explain.
Maybe I am completely wrong, but it seems to me that you are trying to recreate an Animation system.
Is there a specific reason for which you wouldn't use Unity's animation system?
You can do everything that you describe here with it (change sprite, choose animation speed). And you would have almost no code to write. Just drag and drop you sprites in the editor for a start
EDIT - answer to first comment:
What you should do is create all the animations you need. Then in the animator, you choose which condition will trigger a transition to another animation (for instance, boolean jump is true will transition to animation jump). Then in your code you can call GetComponent().SetBool("Jump", true).
To have different character, you can create different gameObjects (one per character). They all have a different animator with animations specific to the character.
The other solutiojn if you really want one one gameObject and one animator is that you also add string condition to you animation (example, if character=="character1" and jump==true, transition to jump animation).
If I were you I would really focus on testing and learning all you can do with Unity animator. I can't think of a case were you would need to recreate the animation system yourself
Your question was long winded and hard to understand but ill try to help,
firstly if you want editable values in the unity editor I would Suggest using a serialized structure of information like this
[System.Serializable] // this makes it viewable in the inspector
public struct Sprite_Manager;
{
public Sprite[] Render_Sprites; // array of sprites to store sprites
public SpriteRenderer S_Renderer;
public float Anim_Delay;
}
public class Character : MonoBehavior {
Sprite_Manager SMG = new Sprite_Manager(); // instantiate a new instance of the struct
void Set_Sprite()
{
for(int i = 0; i < SMG.Render_Sprites.Length; i++)
{
SMG.S_Renderer.sprite = SMG.Render_Sprites[i];
}
}
void Update
{
Invoke("Set_Sprite", SMG.Anim_Delay);
}
}
Not sure if this is exactly what your looking for but this is one way you could setup a structure of sprite based information and use Invoke to setup some sort of delay when passing new sprites to the renderer.

Does culling affect animation

I just simply want to know that does culling affect the animation cause i have some culled animation object which some time work and some time not as I move my camera in the scene?
I search on google I found AnimatorCullingMode.then I check my animated object in culling type basedOnredere is selected. Do I need to change it to alwaysAnimate.
After working a 4/5 days on this issue, Finally I got the answer(silly me).
Yes culling affect the animation using animation component's property which is called Culling Type as Figure show.
Previously My all animation Culling type were selected to BasedOnRenderers that's why my some animation working while some were playing continuously. As I got that:
BasedOnRenderers: Animation is disabled when renderers are not visible.
This culling method is more suitable when you have renderers attached after import - it will take renderers (like mesh renderers, particle renderers and so on) attached to this gameObject or children of this game object.
while the option I now selected which have solve the Problem (Al-Hamdullilah)
said:
Always Animate: Animation culling is disabled - object is animated even when offscreen.

Unity - How do I apply an animation from one game object to another rigged game object?

I am thinking of building a game like the sims and was thinking of how I could make the characters interact with any object without having to systematically change the character's code and prefab whenever I decide to add new furniture and props to the library. I have had an idea that consists of having my animations attached to the furniture itself rather than having it attached to the character (which would require a lot of memory and extra code if we had a lot of furniture in the game library!). I was thinking about it and had in mind that I could add a rig skeleton in Blender that has the desired animation(s) but not rig any specific object - so that I only have the animation, the location, rotation and scale of the bones! and since the characters' rigs are the same as the skeleton's rig in the furniture's animation, they could be compatible!
I hope this makes sense...
The thing is that I realise at this stage that I do not know the Animation class very well, and I am not sure if / how I can use the animation attached to the furniture to make my character interact with it...
Can Animation.Play() take multiple arguments apart from the animation to play and the play mode? Is it possible to specify which object has to play the animation? I am talking about doing all of this from the script - I write in C# by the way... and I might attach the script to the camera so I hope the script does not have to be necessarily attached to the character prefab in order to play the animation... Though I don't mind creating a function in the character's script if necessary!
I am not new to Unity but I have mostly been using cars and character controllers, so I haven't dealt with much animation coding yet...
I thank you in advance for your help and apologise for my weird english - I live in France! ^^
Mecanim performs retargeting of humanoid animations, so for what concern that specific case, you should be able to use the same rig and set of animation clip on different models.
For what concern grabbing furniture, you don't need the object to be rigged. You can use IK to precise move your character's hand toward the target object and then parent it to the relative joint in the hierarchy.

Categories

Resources