06 动画效果Animation

1. 站立动画

1. 给角色添加动画组件:

Add Conponent 中搜索 Animator。在 Assets 文件夹下建立如图所示的文件夹及 Animator Controller 文件:

2. 打开 Animation 窗口

Window - Animation - Animation 就打开了动画窗口。

3. 创建动画效果

点击 Player,然后在 Animation 窗口点 Create,创建一个 Idle 原地站立的动画效果,然后将素材文件夹下的 idle 中的四张图片一次性拖到 Animation 的时间轴窗口中。

但是因为帧与帧之间的时间太短,动画播放的非常快。所以需要将时间轴进行调整,最简单粗暴的方法就是直接拉动时间轴。改变帧之间的时间间隔。另外一种方法还可以调整采样频率 Samples,改小之后也会发现主角的动画变正常了。

2. 移动动画

点击下拉框 Create New Clip,创建一个新的移动动画。

同样的,把 run 动画也拖拽到时间轴上。

3. 动画控制机

Window - Animation - Animator

打开之后,会发现我们创建的几个动画都在界面中。Run 和 Idle 之间肯定有转化的条件,也就是二者之间都有某种关系,那么右键 Run,Make Transition,出现一个白色的箭头,把箭头的指给 Idle,对 Idle 也做同样的事。

创建好联系之后,点击左侧 Parameters 面板的 + 号,添加一个 Float 型的参数 Running。

点击 Idle -> Run 的箭头,在 Inspector 面板中的 Conditions 下点击 + 号新增一个条件:

然后在 Settings 中,取消 Has Exit Time 的勾,将 Transition Duration 设置为0。

同样在 Run -> Idle 中也需要进行同样的设置。

4. 控制 Running 参数

在 Player Controller 脚本中编写代码来控制 Running 参数(即在 Animator 的 Parameter 面板中创建的参数)

首先定义一个 Animator 变量:

public Animator anim;

把之前给 Player 添加的 Animator 组件拖拽到脚本组件下的 anim 上。

在角色移动部分添加:

anim.SetFloat("Running", Mathf.Abs(faceDirrection)); // 绝对值

完整代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour
{
    public Rigidbody2D rb;
    public float speed;
    public float jumpForce;
    public Animator anim;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Movement();
    }

    void Movement(){
        float horizontalMove = Input.GetAxis("Horizontal");
        float faceDirrection = Input.GetAxisRaw("Horizontal");
        // 角色移动
        rb.velocity = new Vector2(horizontalMove * speed * Time.deltaTime, rb.velocity.y);
        anim.SetFloat("Running", Mathf.Abs(faceDirrection)); // 绝对值
        if(faceDirrection != 0){
            transform.localScale = new Vector3(faceDirrection, 1, 1);
        }

        // 角色跳跃
        if(Input.GetButtonDown("Jump")){
            rb.velocity = new Vector2(rb.velocity.x, jumpForce * Time.deltaTime);
        }
    }
}

角色在移动的时候就有移动动画了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值