Unity3d animation operation and animation implementation, unity3d animation implementation
Today we mainly summarize the built-in animation operations in unity3d and the implementation of code event writing animations.
1. How to import and execute external Animation
In the project window, click Select the animation model we have prepared, and select the Animations column in the property panel,
Click "+", "-" in the attribute to add or delete an animation clip,
In Start and End, you can set the Start and End frames of each animation clip respectively.
Below is a breakdown of my own animation:
Idle: The preparation action in my animation.
Aim: indicates the aiming action in my animation.
Fire: indicates the shooting Action in my action.
After the animation fragment is set up above, we use code to implement it. How can we control the fragment to complete an animation process by pressing "1" on the keyboard?
First, how to set a button:
On the menu bar, choose Edit> roject Setting> Input.
Select an item in the Input attribute panel
Set Name to Play
Negative Button (press the key) to set it to the number key "1";
Positive Button (release key) is set to number key "1";
As shown in:
Okay. We have set the buttons on the top.
Next, how can we use code to control these fragments by pressing "1" on the keyboard to complete an animation process?
See the code below:
- // Controllable
- Bool CanControl = true;
- // Update is called once per frame
- Void Update ()
- {
- // When you press play, that is, the number key "1" We set above.
- If (Input. GetButtonDown ("Play "))
- {
- // Execute an animation clip called "Aim", that is, the targeted animation clip we set above.
- GameObject. animation. PlayQueued ("Aim ");
- }
- // When the key 1 is pressed and controllable
- Else if (Input. GetButtonUp ("Play") & CanControl)
- {
- // Execute the animation fire, which is the shot animation clip we set above.
- GameObject. animation. PlayQueued ("Fire ");
- // Continue to execute the Idle, which is the prepared animation clip we set above.
- GameObject. animation. PlayQueued ("Idle ");
- CanControl = false;
- }
- CanControl = true;
- }
Copy code
Copy code
Through the simple code above, we can achieve the series of animation decomposition fragments.
See the following:
1. Press the keyboard number key "1" to execute the targeted action, an animated clip of "Aim"
2. Release the shooting action carried out by the number key "1" and animated fire
3. Release the preparation action performed by the number key "1", and then continue to execute the Idle
2. Use the unity3d built-in animation System
First, drag and drop the clip to the scene window, set the camera coordinates, and add the parallel light.
In the upper-right corner of the game window, click. In the displayed menu, choose Add Tab> Animation.
We will find that there is an Animation tab in the game window.
Next, create an Animation. Click the Animation tab and go
Save the animation in the pop-up form.
On the Animation tab, we find that there is a short menu bar, which is explained from left to right first.
1: indicates that the animation is adjusted to the previous frame;
2: indicates the next frame of the jump knife animation;
3: enter a text box to indicate any time point
4: Add a key
5: add an event (Code required)
Click "-" next to the selected item. In the displayed menu, select a corresponding curve or key.
Next we add Rotation. y (rotate with y axis), with a value of 360 degrees.
It indicates that our object will rotate at 360 degrees.
In the curve area, press and hold the button to select a region.
After you press the "F" key, you will find that this area will be partially enlarged, which makes it easy for me to view it when there are many curve nodes.
The above briefly introduces the built-in Unity3d animation. The following describes the code event animation.
For example, we first define a camera object.
Then I wrote two public event methods.
CameraFarAway (): In this method, the camera's viewing angle is extended.
CameraClose (): In this method, we achieve a closer camera angle.
Then, add an event animation to the corresponding event node through the above method to achieve a pull-closer animation.
For example:
The angle of view is closer to the forklift rotation, and the clip in front of the forklift falls into the animation
Angle pull forklift rotation, forklift front clip rising Animation
Unity3d code for animation
- // Define a time variable
- Public float time = 5.0f;
- // Use this for initialization
- Void Start (){
- // Define the starting coordinate of an animation curve as-3.023f. After 5.0 seconds, the coordinates are moved to 2.96f.
- AnimationCurve curve = AnimationCurve. Linear (0.0f,-3.023f, time, 2.96f );
- // Add
- Curve. AddKey (2 * time,-3.023f );
- // Create an animation clip
- AnimationClip clip = new AnimationClip ();
- // Set the curve object in the clip to move on the X axis
- Clip. SetCurve ("", typeof (Transform), "localPosition. x", curve );
- // Add the clip to the animation
- Animation. AddClip (clip, "Test ");
- // Play an animation clip named "Test"
- Animation. Play ("Test ");
- // Play the animation cyclically
- Animation. wrapMode = WrapMode. Loop;
- }
- // Update is called once per frame
- Void Update (){
- }
Copy code
Copy code
From the above, we can see that the code creation is similar to setting an internal animation as summarized yesterday]
First, create a curve, and then generate a fragment from the curve, and then generate an animation from the fragment.