Unity3d animation operation and animation implementation

Source: Internet
Author: User




today's main summary is the built-in animation operations in Unity3d and the implementation of code event authoring animations

1. How to import and perform external animations

In the Project window, first, click to select the animated model we prepared, select the animations bar in the properties panel,
Click "+" in the properties, "-" can add and delete animation segments,
in start and end, you can set the number of start frames and end frames for each animation segment individually.
here is a breakdown of my own animations:
Idle: Represents the Ready action in my animation.
AIM: Indicates the aim action in my animation.
Fire : Indicates the shooting action in my action.



when the above animation section is set up, we use the code to implement, how to control these fragments by the number keys on the keyboard "1" to complete an animation process

first, how to set a key:
in the menu bar, select Edit->roject Setting->input
Select an item in the Input properties panel
name set as Play
Negative button (press the key) is set to the number key "1";
Positive button (release key) is set to the number key "1";
as shown in the following:



OK, we've set the button on the top.
How do you use code to control these fragments to complete an animation process by using the number keys "1" on the keyboard?
below, please see the detailed code:
  1. Whether it can be controlled
  2. BOOL Cancontrol=true;
  3. Update is called once per frame
  4. void Update ()
  5. {
  6. When you press play, we set the number key "1".
  7. if (Input.getbuttondown ("Play"))
  8. {
  9. The animated section of the animation called "Aim" is the target animation section we set above
  10. GameObject.animation.PlayQueued ("Aim");
  11. }
  12. When the 1 key is pressed and the control
  13. else if (Input.getbuttonup ("Play") &&cancontrol)
  14. {
  15. Perform animated fire is the shooting animation section we set above
  16. GameObject.animation.PlayQueued ("Fire");
  17. Then we continue to execute Idle, which is what we set up for the animation section.
  18. GameObject.animation.PlayQueued ("Idle");
  19. Cancontrol =false;
  20. }
  21. Cancontrol =true;
  22. }
Copy Code

Copy Code
with the simple code above, we can make the series of animation decomposition fragments.
See below:

1. Press the keyboard number key "1" to perform the targeted action, "aim" animation section



2. Release the shooting action performed by the number key "1", Animated fire



3. Loosen the prepare action performed by the number key "1" and proceed with the Idle



2. Using the Unity3d built-in animation system

First, drag and drop the footage into the scene window, set the camera coordinates, add the parallel light



in the upper-right corner of the game window, click , in the pop-up menu, select Add tab->animation



then we'll find a tab called animation (animated) on the game window.


Next, create an animation, and in the animation (animation) tab, clickafter, in
pop up the form to save our animations.



in the Animation tab, we find that there will be a small section of the menu bar, which we'll explain from left to right
1: A frame that is transferred to the animation;
2: The next frame of the jumping knife animation;
3: Text box input indicates any point in time
4: Add a key
5: Indicates an action to add an event (code written)


You can click the "-" sign after the corresponding selection, then select Add a corresponding curve or key in the pop-up menu



below we add rotation.y (rotated in the Y axis) with a value of 360 degrees.
indicates that our object will rotate objects at 360 degrees.



in the Curve area, left-click and drag to select an area



after pressing "F" key, it will be found that this area will be zoomed in locally, so it is convenient for me to view in the case of more curve nodes




Here's a quick introduction to the built-in animations for the next unity3d, which we'll show you about code event animations



as we first defined the object of a camera
and then wrote 2 public event methods.
Camerafaraway (): This method allows us to pull away from the camera perspective.
Cameraclose (): This method allows us to zoom in from camera perspective

then, using the above method, add an event animation to the corresponding event node to achieve a pull-away animation
such as:

angle of view closer to forklift rotation, forklift front clip Drop animation


angle of view pull away forklift swivel, forklift front clip up animation





Unity3d Code realization animation function

  1. Define a Time variable
  2. public float time=5.0f;
  3. Use this for initialization
  4. void Start () {
  5. Define an animation curve starting position coordinates of -3.023f, after 5 seconds after the coordinates moved to 2.96f
  6. Animationcurve curve=animationcurve.linear (0.0f,-3.023f,time,2.96f);
  7. Add to
  8. Curve. Addkey (2*time,-3.023f);
  9. Create a cartoon segment
  10. Animationclip clip=new Animationclip ();
  11. Sets the Curve object in the fragment to move in the x-axis
  12. Clip. Setcurve ("", typeof (Transform), "localposition.x", curve);
  13. Add the fragment to the animation
  14. Animation. Addclip (clip, "Test");
  15. Play a cartoon section called "Test"
  16. Animation. Play ("Test");
  17. Loop the animation back
  18. Animation.wrapmode =wrapmode.loop;
  19. }
  20. Update is called once per frame
  21. void Update () {
  22. }
Copy Code

Copy Code
you can see that the code creation is similar to the internal setting animation that was summarized yesterday.Dog Planing Learning Net"
a curve is created first and then the fragment is generated by the curve and then the fragment is animated.

Unity3d animation operation and animation implementation

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.