Unity3d animation operation and animation implementation, unity3d animation implementation

Source: Internet
Author: User

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:

  1. // Controllable
  2. Bool CanControl = true;
  3. // Update is called once per frame
  4. Void Update ()
  5. {
  6. // When you press play, that is, the number key "1" We set above.
  7. If (Input. GetButtonDown ("Play "))
  8. {
  9. // Execute an animation clip called "Aim", that is, the targeted animation clip we set above.
  10. GameObject. animation. PlayQueued ("Aim ");
  11. }
  12. // When the key 1 is pressed and controllable
  13. Else if (Input. GetButtonUp ("Play") & CanControl)
  14. {
  15. // Execute the animation fire, which is the shot animation clip we set above.
  16. GameObject. animation. PlayQueued ("Fire ");
  17. // Continue to execute the Idle, which is the prepared animation clip we set above.
  18. GameObject. animation. PlayQueued ("Idle ");
  19. CanControl = false;
  20. }
  21. CanControl = true;
  22. }
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

  1. // Define a time variable
  2. Public float time = 5.0f;
  3. // Use this for initialization
  4. Void Start (){
  5. // Define the starting coordinate of an animation curve as-3.023f. After 5.0 seconds, the coordinates are moved to 2.96f.
  6. AnimationCurve curve = AnimationCurve. Linear (0.0f,-3.023f, time, 2.96f );
  7. // Add
  8. Curve. AddKey (2 * time,-3.023f );
  9. // Create an animation clip
  10. AnimationClip clip = new AnimationClip ();
  11. // Set the curve object in the clip to move on the X axis
  12. Clip. SetCurve ("", typeof (Transform), "localPosition. x", curve );
  13. // Add the clip to the animation
  14. Animation. AddClip (clip, "Test ");
  15. // Play an animation clip named "Test"
  16. Animation. Play ("Test ");
  17. // Play the animation cyclically
  18. Animation. wrapMode = WrapMode. Loop;
  19. }
  20. // Update is called once per frame
  21. Void Update (){
  22. }
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.