对于Unity的动画系统,之前只限于运用在基于人物、骨骼等三维物体上,并没有在二维包括Button、Sprite等上多加运用及思考过。正好现在来补充一下自己的盲区。
-
作用于Button按钮的动画(例如:自定义按钮的悬浮状态)
- 将Button组件的Transition设置为Animation,点击"Auto Generate Animation"按钮,将会创建属于Button的controller;
- 选中该Button,打开Animation窗口,可以添加不同Trigger的效果(直接在第一帧添加关键帧即可)
-
作用于Sprite的逐帧动画
选中要加入动画的图片,拖入到Hierarchy面板,即可创建一个Animtion和AnimatorController。
-
StringToHash
private Animator anim;
private int SpeedID = Animator.StringToHash("Speed");
void Update () {
anim.SetFloat(SpeedID, Input.GetAxis("Vertical"));
}
-
BlendTree的运用(控制角色左跑、右跑,摄像机跟随)
点击参考之前的博客
public class AnimController: MonoBehaviour {
private Animator anim;
private int vertical = Animator.StringToHash("Vertical");
private int horizontal = Animator.StringToHash("Horizontal");
private void Awake()
{
anim = this.GetComponent<Animator>();
}
void Update () {
anim.SetFloat(vertical, Input.GetAxis("Vertical")*5.6f);
anim.SetFloat(horizontal, Input.GetAxis("Horizontal")*1.68f);
}
}
public class FollowPlayer : MonoBehaviour {
public Transform Player;
public Vector3 offset;
private float smoothing = 3;
// Use this for initialization
void Start () {
offset = transform.position- Player.position;
}
void LateUpdate () {
Vector3 targetPos = Player.position + Player.TransformDirection(offset);
transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * smoothing);
transform.LookAt(Player);
}
}