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 ...
随机推荐
- 【查阅】mysql系统视图查看
[1]查看表大小 SELECT CONCAT(table_schema,'.',table_name) AS 'Table Name', table_rows AS 'Number of Rows', ...
- vue学习之三常用命令
一.插值 1.1 +号运用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- Git warning:LF will be replaced by CRLF in readme.txt的原因与解决方案
今天用Git bash遇到的问题,看了几个回答之后发现一个比较有价值的,给大家分享一下,其他很多的回答都有很或多或少存在一些弊端. 原回答地址在stackoverflow上,附上链接--http:// ...
- Centos7 Zabbix3.2集群安装
安装环境:服务器10.80.0.191作为zabbix-server,10.80.0.191-195作为zabbix-agent. [zabbix@miyan ~]$ cat /etc/redhat- ...
- XenServer:使用XenCenter开设VPS(多图完整版)
打铁要趁热,咱们接着来玩XenServer.昨天赵容用机房提供的KVM给服务器装了XenServer,今天我们来玩更有意思的:开小鸡.装好XenServer之后,访问我们的服务器IP,就可以看到Xen ...
- linux命令:linux文件处理命令
命令格式 : 命令 [-选项] [参数] 例:ls -la /etc 说明:1)个别命令使用不遵循此格式,[]代表可选 2)当有多个选项时,可以写在一起 3)-a等于 --all,调用简化选项用 ...
- Js中split()方法的正确使用
通过 js 获取 QueryString (location.search部分) 参数很常见,网上代码也满天飞.不过现在的框架,基本上都通过路由伪静态了,把以前的 QueryString 变成了pat ...
- JQuery中如何使用事件来出发Ajax
$(document).ready(function(){ $("input[name='customer_name']").keydown(func ...
- no-siteapp 和 no-transform
简单的说,是禁止转码 . 举个通俗的例子. 你建了一栋房子(网站),百度说我给你做个大门,但是大门上要有我的广告 你不愿意,就建立了一条路叫no-transform 别人去你家走这条路就行了 后来百度 ...
- [SQL入门级] 这篇咱'薄利多销',记录多一点
这个系列的博文知识回顾sql知识作的记录,温故而知新.上一篇内容达不到知识分享的层级被移出园子首页,对不住各位看官,内容简单了些.下面咱就记录多一些的基础知识,薄利多销: 控制用户权限 • 创建用户 ...