Unity3D学习笔记(十六):Animator新动画
设置默认片段

建立基础连线

任何状态都可以连线到death,甚至包括自己到自己


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HellephantAnimator : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Alpha1))
{
anim.SetBool("isMove", true);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
anim.SetBool("isMove", false);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
anim.SetTrigger("Death");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public enum EnemySta
{
Move,
Idle,
Death
}
public class NewMonsterBase : MonoBehaviour {
public PlayerControl moveTarget;//设置目标点
public MonsterManager manager;//怪物属于那个管理器
public virtual void Action()
{
}
public virtual void Move()
{
}
public virtual void Idle()
{
}
public virtual void Damage()
{
}
public virtual void Death()
{
}
public virtual void DeathEvent()
{
}
}
这两个需要初始化,要放在父类里
public PlayerControl moveTarget;//设置目标点
public MonsterManager manager;//怪物属于那个管理器
子类:继承父类,并重写父类方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class HellephantControl : NewMonsterBase
{
public Animator anim;
public EnemySta enemySta;
//因为我们需要经常用到目标的状态,所以把TransForm改成了PlayerControl
public NavMeshAgent navMeshAgent;//引用寻路系统
private int enemyHealth = ;//设置怪物生命值
// Use this for initialization
void Start()//子类添加新动画系统
{
enemySta = EnemySta.Move;
anim.SetBool("isMove", true);
}
// Update is called once per frame
void Update()
{
Action();
}
public override void Action()
{
switch (enemySta)
{
case EnemySta.Move:
Move();
break;
case EnemySta.Idle:
Idle();
break;
case EnemySta.Death:
Death();
break;
}
}
public override void Move()
{
//anim.CrossFade("Move", 0.2f);
navMeshAgent.SetDestination(moveTarget.transform.position);
//如果怪物的目标死了
if (moveTarget.playSta == PlaySta.Death)
{
//寻路停止
navMeshAgent.isStopped = true;
//状态切换到闲置
enemySta = EnemySta.Idle;
anim.SetBool("isMove", false);
}
}
public override void Idle()
{
//anim.CrossFade("Idle", 0.2f);
}
public override void Damage()
{
//受伤减血
enemyHealth -= ;
//Debug.Log("怪物生命"+enemyHealth);
//如果血到了0
if (enemyHealth <= )
{
//状态变成死亡状态
enemySta = EnemySta.Death;
anim.SetTrigger("Death");
navMeshAgent.isStopped = true;
gameObject.GetComponent<SphereCollider>().enabled = false;
//gameObject.tag = "Untagged";
}
}
public override void Death()//子类关闭老动画系统
{
//anim.CrossFade("Death", 0.2f);
}
public override void DeathEvent()
{
//在管理器列表中移除自己
manager.monsterList.Remove(this);
//销毁自己
Destroy(this.gameObject);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MounsterManager : MonoBehaviour {
List<Transform> birthPoint;
public GameObject[] monsterPreGo;
//最大怪物数
public int MaxMonsterCount;
public List<NewMonsterBase> monsterList;
public NewPlayer player;
// Use this for initialization
void Start () {
birthPoint = new List<Transform>();
monsterList = new List<NewMonsterBase>();
//遍历所有的子物体,将其加到出生点列表中
for (int i = ; i < transform.childCount; i++)
{
birthPoint.Add(transform.GetChild(i));
} }
void CreateMonster() {
monsterCont++;
GameObject go;
if (monsterCont%==)
{
go = Instantiate<GameObject>(monsterPreGo[]);
}
else
{
//实例化一个怪物
go = Instantiate<GameObject>(monsterPreGo[]);
} //怪物的位置设置任意出生点
go.transform.position = birthPoint[Random.Range(, birthPoint.Count)].position;
//取得怪物脚本
NewMonsterBase monster = go.GetComponent<NewMonsterBase>();
//把怪物放到管理器列表里
monsterList.Add(monster);
//指定怪物的目标
monster.moveTarget = player;
//指定怪物的管理者
monster.manager = this;
}
int monsterCont = ;
void CheckMonster() {
if (monsterList.Count < MaxMonsterCount)
{
CreateMonster();
}
}
// Update is called once per frame
void Update () { }
private void FixedUpdate()
{
CheckMonster();
}
}



怪物添加碰撞体

动画和模型分开

拖拽模型生成预制体

添加动画控制器

右键新建 融合树

双击融合树,打开融合树窗口,右键可以再添加融合树

添加motion

旧动画系统修改动画片段的速度

添加脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefaultAvatarAnimator : MonoBehaviour {
public Animator anim;
public int speedHashID;
public int angleHashID;
public int attackHashID;
private float slowSpeed;//速度修正
// Use this for initialization
void Start () {
speedHashID = Animator.StringToHash("Speed");//哈希Code,效能略高,不用每次调用都转换
angleHashID = Animator.StringToHash("Angle");
attackHashID = Animator.StringToHash("Attack");
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.LeftShift))
{
slowSpeed = 0.5F;
}
else
{
slowSpeed = 1.0F;
}
anim.SetFloat(speedHashID, Mathf.Clamp01(Input.GetAxis("Vertical")) * slowSpeed);//Mathf.Clamp01,剔除向后走的值
anim.SetFloat(angleHashID, Input.GetAxis("Horizontal") * slowSpeed);
if (Input.GetMouseButtonDown())
{
anim.SetTrigger(attackHashID);
} }
}
给Walk和Run添加融合树


点击齿轮,修改层级权重为1

添加代码

Unity3D学习笔记(十六):Animator新动画的更多相关文章
- python3.4学习笔记(十六) windows下面安装easy_install和pip教程
python3.4学习笔记(十六) windows下面安装easy_install和pip教程 easy_install和pip都是用来下载安装Python一个公共资源库PyPI的相关资源包的 首先安 ...
- (C/C++学习笔记) 十六. 预处理
十六. 预处理 ● 关键字typeof 作用: 为一个已有的数据类型起一个或多个别名(alias), 从而增加了代码的可读性. typedef known_type_name new_type_nam ...
- python 学习笔记十六 django深入学习一 路由系统,模板,admin,数据库操作
django 请求流程图 django 路由系统 在django中我们可以通过定义urls,让不同的url路由到不同的处理函数 from . import views urlpatterns = [ ...
- SharpGL学习笔记(十六) 多重纹理映射
多重纹理就把多张贴图隔和在一起.比如下面示例中,一个表现砖墙的纹理,配合一个表现聚光灯效果的灰度图,就形成了砖墙被一个聚光灯照亮的效果,这便是所谓的光照贴图技术. 多重纹理只在OpenGL扩展库中才提 ...
- yii2源码学习笔记(十六)
Module类的最后代码 /** * Registers sub-modules in the current module. * 注册子模块到当前模块 * Each sub-module shoul ...
- Swift学习笔记十六:协议
Protocol(协议)用于统一方法和属性的名称,而不实现不论什么功能. 协议可以被类.枚举.结构体实现.满足协议要求的类,枚举,结构体被称为协议的遵循者. 遵循者须要提供协议指定的成员,如属性,方法 ...
- PHP学习笔记十六【方法】
<?php //给一个函数传递基本数据类型 $a=90; $b=90.8; $c=true; $d="hello world"; function test1($a,$b,$ ...
- Java基础学习笔记十六 集合框架(二)
List List接口的特点: 它是一个元素存取有序的集合.例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的. 它是一个带有索引的集合,通过索引就可以精 ...
- Java学习笔记十六:Java中的构造方法
Java中的构造方法 1.使用new+构造方法 创建一个新的对象: 2.构造方法是定义在Java类中的一个用来初始化对象的方法: 3.构造方法与类同名且没有返回值: 4.语法格式: public 构造 ...
- JavaScript权威设计--CSS(简要学习笔记十六)
1.Document的一些特殊属性 document.lastModified document.URL document.title document.referrer document.domai ...
随机推荐
- 用SCSS需要小心IE对css的几个限制
IE对CSS的限制主要有两个: 一个页面中引用的CSS只读前32个 一个CSS文件中只读前4095个选择器 关于这个问题的文章有很多,我就不细讲了. 我想讲的是在用SCSS写CSS的时候非常容易超过这 ...
- Window版本 安装mysql
#1.下载:MySQL Community Server 5.7.16 http://dev.mysql.com/downloads/mysql/ 下载下来解压到指定目录 就安装完成了 #2.解压 如 ...
- phpsocketclient以及server样例
一个菜鸟朋友,突然问了我这个问题...如今稍稍有点时间,就写了一个简单的样例给他,顺便贴上来 server端: <? php /** * @author 邹颢 zouhao619@gmail.c ...
- 2017php经典面试题
1.PHP语言的一大优势是跨平台,什么是跨平台?一.PHP基础: PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配置,不 ...
- 万恶之源 - Python基础知识补充
编码转换 编码回顾: 1. ASCII : 最早的编码. ⾥⾯有英⽂⼤写字⺟, ⼩写字⺟, 数字, ⼀些特殊字符. 没有中⽂, 8个01代码, 8个bit, 1个byte 2. GBK: 中⽂国标码, ...
- 去掉python的警告
1.常规警告 import warnings warnings.filterwarnings("ignore") 2.安装gensim,在python中导入的时候出现一个警告: w ...
- unity3d-碰撞检测
碰撞检测 游戏中很多时候都要判断碰撞检测,比如子弹打中敌机.当碰撞后.就要发生爆炸. 或者敌机减血, 我们先看一张图片,看皮球从天空下落.与地面碰撞的过程 碰撞检测条件 游戏中两个对象发生碰撞是需要条 ...
- 怎么在jquery里清空文本框的内容
$("input[name='test']").val("").focus(); // 将name=test的文本框清空并获得焦点,以便重新输入
- 不用中间变量交换a 和b的值
// 不用中间变量的写法 ,假如 a=13, b=8; a=a+b =21; //此时 a=21; b=8; b=a-b=13; //此时a=21; b=13; a=a-b=8; //相当于 a=21 ...
- C++矩阵库 Eigen 快速入门
最近需要用 C++ 做一些数值计算,之前一直采用Matlab 混合编程的方式处理矩阵运算,非常麻烦,直到发现了 Eigen 库,简直相见恨晚,好用哭了. Eigen 是一个基于C++模板的线性代数库, ...