UNITY中做动画的两个不错的连接

Inverse Kinematics

Unity - Manual: Inverse Kinematics

Most animation is produced by rotating the angles of joints in a skeleton to predetermined values. The position of a child joint changes according to the rotation of its parent and so the end point of a chain of joints can be determined from the angles and relative positions of the individual joints it contains. This method of posing a skeleton is known as forward kinematics.

However, it is often useful to look at the task of posing joints from the opposite point of view - given a chosen position in space, work backwards and find a valid way of orienting the joints so that the end point lands at that position. This can be useful when you want a character to touch an object at a point selected by the user or plant its feet convincingly on an uneven surface. This approach is known as Inverse Kinematics (IK) and is supported in Mecanim for any humanoid character with a correctly configured Avatar.

To set up IK for a character, you typically have objects around the scene that a character interacts with, and then set up the IK thru script, in particular, Animator functions like SetIKPositionWeight, SetIKRotationWeight, SetIKPosition, SetIKRotation, SetLookAtPosition, bodyPosition, bodyRotation

In the illustration above, we show a character grabbing a cylindrical object. How do we make this happen?

We start out with a character that has a valid Avatar.

Next create an Animator Controller with containing at least one animation for the character. Then in the Layers pane of the Animator window, click the cog settings icon of the Layer and and check the IK Pass checkbox in the menu which pops up.

Next, attach to it a script that actually takes care of the IK, let's call it IKControl. This script sets the IK target for the character's right hand, and its look position to make it look at the object it is holding:

cs 复制代码
using UnityEngine;
using System;
using System.Collections;

[RequireComponent(typeof(Animator))] 

public class IKControl : MonoBehaviour {
    
    protected Animator animator;
    
    public bool ikActive = false;
    public Transform rightHandObj = null;
    public Transform lookObj = null;

    void Start () 
    {
        animator = GetComponent<Animator>();
    }
    
    //a callback for calculating IK
    void OnAnimatorIK()
    {
        if(animator) {
            
            //if the IK is active, set the position and rotation directly to the goal. 
            if(ikActive) {

                // Set the look target position, if one has been assigned
                if(lookObj != null) {
                    animator.SetLookAtWeight(1);
                    animator.SetLookAtPosition(lookObj.position);
                }    

                // Set the right hand target position and rotation, if one has been assigned
                if(rightHandObj != null) {
                    animator.SetIKPositionWeight(AvatarIKGoal.RightHand,1);
                    animator.SetIKRotationWeight(AvatarIKGoal.RightHand,1);  
                    animator.SetIKPosition(AvatarIKGoal.RightHand,rightHandObj.position);
                    animator.SetIKRotation(AvatarIKGoal.RightHand,rightHandObj.rotation);
                }        
                
            }
            
            //if the IK is not active, set the position and rotation of the hand and head back to the original position
            else {          
                animator.SetIKPositionWeight(AvatarIKGoal.RightHand,0);
                animator.SetIKRotationWeight(AvatarIKGoal.RightHand,0); 
                animator.SetLookAtWeight(0);
            }
        }
    }    
}

As we do not intend for the character's hand to reach inside the object to its centre (the cylinder's pivot point), we position an empty child object (in this case, named "Cylinder Grab Handle") where the hand should be on the cylinder, and rotate it accordingly. The hand then targets this child object.

How to Create Animations from Models

How To Create Animations From Models And Sprites Within Unity - GameDev Academy

Click on the play icon (the red circle) to get started.

Click on the arrow to go to the next keyframe and this time, we will change the rotation in the Animation window instead of the character in the hierarchy. Change the Y rotation to -58 and 58 respectively.

We will continue to do this in 0.10 second clips until the Y rotation value reaches 28.

But wait...There's more! We aren't done with this animation clip yet. We now have to take all the frames we made and do the reverse in this clip.

Now that we have done all of that, we can deselect that red play button and watch it in game view. The Animation is automatically saved now. But always save your project and scenes often.

As you can see, it is not difficult to do the animations. The only issue I have is that it can be extremely tedious to do and depending on what you want to do with the animations can make it become a bit unmanageable at times.

相关推荐
8 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
马特说12 小时前
Unity VR手术模拟系统架构分析与数据流设计
unity·系统架构·vr
心前阳光1 天前
Unity WebGL文本输入
unity·游戏引擎·webgl
天涯过客TYGK1 天前
unity A星寻路
unity·游戏引擎
KhalilRuan1 天前
Unity Demo——3D平台跳跃游戏笔记
笔记·游戏·unity·游戏引擎
ttod_qzstudio2 天前
Unity中使用EzySlice实现模型切割与UV控制完全指南
unity
南無忘码至尊2 天前
Unity 实现与 Ollama API 交互的实时流式响应处理
unity·游戏引擎·交互
平行云3 天前
如何实现UE程序大并发多集群的像素流部署
unity·ue5·图形渲染
向宇it3 天前
【unity小技巧】在 Unity 中将 2D 精灵添加到 3D 游戏中,并实现阴影投射效果,实现类《八分旅人》《饥荒》等等的2.5D游戏效果
游戏·3d·unity·编辑器·游戏引擎·材质
向宇it3 天前
Unity Universal Render Pipeline/Lit光照材质介绍
游戏·unity·c#·游戏引擎·材质