一个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 updatevoid Start(){anniController = GetComponent();}// Update is called once per framevoid Update(){PlayAni();}void PlayAni(){if (Input.GetKey(KeyCode.A))anniController.SetBool("cubeMove ", true);if (Input.GetKey(KeyCode.B))anniController.SetBool("sphereMove", true);}}
|