一个Annimator及脚本控制多个物体动画的播放与切换

一个Annimator及脚本控制多个物体动画的播放与切换

创建一个空物体,点击Add Component添加Annimator组件,添加脚本组件;
在这里插入图片描述
创建AnnimatorController文件夹,右击创建AnnimatorController;
在这里插入图片描述
新建一个长方体和一个球体;
在这里插入图片描述
创建Annimation文件夹,并创建annimation “cubeMove”;
在这里插入图片描述
双击all.controller,打开Annimator窗口,将“cubeMove”拖到状态机,并右击Make Transition进行连接;
在这里插入图片描述
Annimator窗口中左方点击Paramaters创建bool 变量cubeMove;
在这里插入图片描述
选中New State到CubeMove的线,在Annimator窗口中右方点击加号添加条件;
在这里插入图片描述
点击空物体all对象进入空物体层级,然后点击annimation “cubeMove”,再点击cube对象录制长方体的动画;
按照同样步骤再创建一个球体的动画;
录制动画时注意下方中由cubeMove切换到sphereMove;
在这里插入图片描述
脚本代码如下(实现按键播放):

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

public class play : MonoBehaviour
{
    Animator anniController;
    // Start is called before the first frame update
    void Start()
    {
        anniController = GetComponent<Animator>();
    }

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

    void PlayAni()
    {
        if (Input.GetKey(KeyCode.A))
            anniController.SetBool("cubeMove ", true);

        if (Input.GetKey(KeyCode.B))
            anniController.SetBool("sphereMove", true);


    }

}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值