新动画系统:
反向动力学动画(IK功能):
魔兽世界(头部动画),神秘海域(手部动画),人类一败涂地(手部动画)
如何启用(调整)
1、必须是新动画系统Animator

设置头、手、肘的目标点

2、动画类型必须是Humanoid,除此之外其他类型都不可以

3、动画系统对应层级的IKPass必须开启

4、相应的IK调整方法只能写在OnAnimatorIK(脚本挂载和Animator同一级别)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefaultAvatarIK : MonoBehaviour {
public Animator anim;
public Transform lookPoint;
public Transform HandPoint;
public Transform ElbowPoint;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
private void OnAnimatorIK(int layerIndex)
{
//用代码调整头部看向的方向
anim.SetLookAtPosition(lookPoint.position);
//调整IK动画的权重
//如果是1代表完全按代码逻辑播放动画(完全融合)
//如果是0完全按原动画播放
anim.SetLookAtWeight();
//调整四肢IK的目标点
anim.SetIKPosition(AvatarIKGoal.LeftHand, HandPoint.position);//AvatarIKGoal是枚举
anim.SetIKPositionWeight(AvatarIKGoal.LeftHand, );
//调整四肢IK关节的目标点
anim.SetIKHintPosition(AvatarIKHint.RightElbow, ElbowPoint.position);
anim.SetIKHintPositionWeight(AvatarIKHint.RightElbow, );
//调整四肢IK的朝向
//anim.SetIKRotation();
}
}

取消物体描边

粒子系统:
Particle System一统江湖,主流离子发射器思想,调整发射器参数发射离子,如AE
Legacy都是老的粒子系统

一个粒子效果由若干个Particle System构成

修改大小
我们没有办法通过GameObject的Scale改大小,Scale只是改变发射区域的大小
可以通过StartSize

可以通过SizeOverLife曲线

启用碰撞
碰撞系统,火焰溅射效果
打开粒子的Collision功能,选择Type为World,平面改3D

也可以选择Type为Planes,设置一个平面触发效果

拖尾效果

脚本使用:播放,停止,销毁

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticleTest : MonoBehaviour {
public ParticleSystem ps;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Alpha1))
{
//调用粒子系统播放
ps.Play();
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
//调用粒子系统停止
ps.Stop();
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
if (ps.isStopped)
{
//失活粒子
gameObject.SetActive(false);
}
}
}
}

重载,连同子级一起处理,填flase只负责自身

取消唤醒,改为代码播放

 
塔防游戏:
怪物管理器:
添加路点(路点放在拐点处),给怪物子类添加路点列表
currentPathNodeID:记录当前路点的变量
pathNodeList.Count-1:路点的最后一个点
移动逻辑
数组越界问题:调整currentPathNodeID++的位置到最后
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonsterA : MonsterBase {
public Animator anim;
public Transform pathParent;
public List<Transform> pathNodeList;
public int currentPathNodeID;
public float speed;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
//初始化路点列表
pathNodeList = new List<Transform>();
//把路点导入到路点列表
for (int i = ; i < pathParent.childCount; i++)
{
pathNodeList.Add(pathParent.GetChild(i));
}
//把自己放在路点的第一个位置
transform.position = pathNodeList[].position;
currentPathNodeID = ;
monsterSta = MonsterSta.Move;
anim.SetBool("isMove", true); } // Update is called once per frame
void Update () {
#region 动画测试
//if (Input.GetKeyDown(KeyCode.Alpha1))
//{
// monsterSta = MonsterSta.Move;
// anim.SetBool("isMove", true);
//}
//if (Input.GetKeyDown(KeyCode.Alpha2))
//{
// monsterSta = MonsterSta.Idle;
// anim.SetBool("isMove", false);
//}
//if (Input.GetKeyDown(KeyCode.Alpha3))
//{
// monsterSta = MonsterSta.Death;
// anim.SetTrigger("Death");
//}
#endregion
Action();
}
public override void Action()
{
switch (monsterSta)
{
case MonsterSta.Idle:
Idle();
break;
case MonsterSta.Move:
Move();
break;
case MonsterSta.Death:
Death();
break;
default:
break;
}
}
public override void Move()
{
//如果怪物还没有到达路点中最后一个点
if (currentPathNodeID < pathNodeList.Count-)
{
//向下一个点前进
float distance = Vector3.Distance(transform.position, pathNodeList[currentPathNodeID + ].position);
transform.position = Vector3.Lerp(transform.position, pathNodeList[currentPathNodeID + ].position, speed/ distance*Time.deltaTime);
//如果我离下一个点的距离到达某一个值 Quaternion targetRot = Quaternion.LookRotation(pathNodeList[currentPathNodeID + ].position - pathNodeList[currentPathNodeID].position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, 0.1f);
//transform.LookAt(pathNodeList[currentPathNodeID + 1]);
if (distance < speed * Time.deltaTime)
{
//改变我的当前点,进而改变目标点,成下一个点
currentPathNodeID++;
}
}
}
}

保留原动画状态机的逻辑,可以替换原动画片段

机枪塔,需要瞄准,攻击速快
炮塔,不用瞄准,攻速慢
塔基
外层(空物体):缩放(1,1,1)
内层(Tower_Base):缩放(0.4,0.4,0.4)
外层添加刚体

内层添加球形碰撞体,勾选Is Trigger

塔基父类代码逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunBase : MonoBehaviour {
public float attackRange;
public MonsterBase tragetMonster;
public SphereCollider attackTrigger;
// Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
public virtual void Indit() {
attackTrigger.radius = attackRange;
}
public virtual void Attack() {
}
}

塔基子类代码逻辑

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunB : GunBase {
public Transform gunPos;
public ParticleSystem ps;
// Use this for initialization
void Start () {
Indit();
} // Update is called once per frame
void Update () {
Attack();
fireCDTime += Time.deltaTime;
}
public override void Attack()
{
if (tragetMonster!=null)
{
if (AttackCheck())
{
if (fireCDTime> fireCD)
{
Fire();
}
}
else
{
RotatGun();
}
}
else
{
ps.Stop();
}
}
public void RotatGun() {
Vector3 dir = tragetMonster.transform.position - gunPos.position;
dir.y = ;
Quaternion targetRot = Quaternion.LookRotation(dir);
gunPos.rotation = Quaternion.Slerp(gunPos.rotation, targetRot, 0.5f);
}
float fireCD = 0.1f;
float fireCDTime;
public void Fire() {
fireCDTime = ;
tragetMonster.Damage();
ps.Play();//特效代码逻辑
}
public bool AttackCheck() {
Vector3 monsterDir = tragetMonster.transform.position - gunPos.position;
monsterDir.y = ;
if (Vector3.Angle(gunPos.forward, monsterDir)<)
{
return true;
}
return false;
}
//如果有物体进入我的攻击范围
private void OnTriggerEnter(Collider other)
{
//如果我没有目标
if (tragetMonster==null)
{
//如果进入我攻击范围的Collider标签是"Monster"
if (other.tag == "Monster")
{
//把这个Monster设置成我的目标
tragetMonster = other.GetComponent<MonsterBase>();
}
}
}
//如果有物体离开我的攻击范围
private void OnTriggerExit(Collider other)
{
//如果我有目标
if (tragetMonster != null)
{
//如果离开的目标是我的目标
if (tragetMonster == other.GetComponent<MonsterBase>())
{
//我的目标为空
tragetMonster = null;
}
}
}
}

设置攻击范围

塔基父类代码逻辑:设置Indit初始化,给碰撞器添加攻击范围

public virtual void Indit() {
attackTrigger.radius = attackRange;
}
攻击检测:炮塔转向
球形差值,正负转向
线性差值,只能正向
    public override void Attack()
{
if (tragetMonster!=null)
{
if (AttackCheck())
{
if (fireCDTime> fireCD)
{
Fire();
}
}
else
{
RotatGun();
}
}
else
{
ps.Stop();
}
} public void RotatGun() {
Vector3 dir = tragetMonster.transform.position - gunPos.position;
dir.y = ;
Quaternion targetRot = Quaternion.LookRotation(dir);
gunPos.rotation = Quaternion.Slerp(gunPos.rotation, targetRot, 0.5f);
}
目标点不对的问题:
dir.y = 0;转向方法的y轴清零
monsterDir.y = 0;攻击检测的角度也要清零
    public void RotatGun() {
Vector3 dir = tragetMonster.transform.position - gunPos.position;
dir.y = ;
Quaternion targetRot = Quaternion.LookRotation(dir);
gunPos.rotation = Quaternion.Slerp(gunPos.rotation, targetRot, 0.5f);
} float fireCD = 0.1f;
float fireCDTime;
public void Fire() {
fireCDTime = ;
tragetMonster.Damage();
ps.Play();//特效代码逻辑
} public bool AttackCheck() {
Vector3 monsterDir = tragetMonster.transform.position - gunPos.position;
monsterDir.y = ;
if (Vector3.Angle(gunPos.forward, monsterDir)<)
{
return true;
}
re

Unity3D学习笔记(十七):IK动画、粒子系统和塔防的更多相关文章

  1. python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容

    python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容 Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖 ...

  2. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  3. unity3d学习笔记(一) 第一人称视角实现和倒计时实现

    unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...

  4. Unity3D学习笔记2——绘制一个带纹理的面

    目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...

  5. Unity3D学习笔记3——Unity Shader的初步使用

    目录 1. 概述 2. 详论 2.1. 创建材质 2.2. 着色器 2.2.1. 名称 2.2.2. 属性 2.2.3. SubShader 2.2.3.1. 标签(Tags) 2.2.3.2. 渲染 ...

  6. Unity3D学习笔记4——创建Mesh高级接口

    目录 1. 概述 2. 详论 3. 其他 4. 参考 1. 概述 在文章Unity3D学习笔记2--绘制一个带纹理的面中使用代码的方式创建了一个Mesh,不过这套接口在Unity中被称为简单接口.与其 ...

  7. Unity3D学习笔记6——GPU实例化(1)

    目录 1. 概述 2. 详论 3. 参考 1. 概述 在之前的文章中说到,一种材质对应一次绘制调用的指令.即使是这种情况,两个三维物体使用同一种材质,但它们使用的材质参数不一样,那么最终仍然会造成两次 ...

  8. Unity3D学习笔记7——GPU实例化(2)

    目录 1. 概述 2. 详论 2.1. 实现 2.2. 解析 3. 参考 1. 概述 在上一篇文章<Unity3D学习笔记6--GPU实例化(1)>详细介绍了Unity3d中GPU实例化的 ...

  9. Unity3D学习笔记8——GPU实例化(3)

    目录 1. 概述 2. 详论 2.1. 自动实例化 2.2. MaterialPropertyBlock 3. 参考 1. 概述 在前两篇文章<Unity3D学习笔记6--GPU实例化(1)&g ...

  10. Unity3D学习笔记12——渲染纹理

    目录 1. 概述 2. 详论 3. 问题 1. 概述 在文章<Unity3D学习笔记11--后处理>中论述了后处理是帧缓存(Framebuffer)技术实现之一:而另外一个帧缓存技术实现就 ...

随机推荐

  1. 几种常见web攻击手段及其防御方式

    XSS(跨站脚本攻击) CSRF(跨站请求伪造) SQL注入 DDOS web安全系列目录 总结几种常见web攻击手段极其防御方式 总结几种常见的安全算法 XSS 概念 全称是跨站脚本攻击(Cross ...

  2. 【Python】easygui小甲鱼

    翻译改编自官方文档:http://easygui.sourceforge.net/tutorial/index.html 翻译改编者:小甲鱼,本文欢迎转载,转载请保证原文的完整性! 演示使用 Pyth ...

  3. MySQL中MyISAM与InnoDB区别及选择,mysql添加外键

    InnoDB:支持事务处理等不加锁读取支持外键支持行锁不支持FULLTEXT类型的索引不保存表的具体行数,扫描表来计算有多少行DELETE 表时,是一行一行的删除InnoDB 把数据和索引存放在表空间 ...

  4. makefile 中wildcard

    在Makefile规则中,通配符会被自动展开.但在变量的定义和函数引用时,通配符将失效.这种情况下如果需要通配符有效,就需要使用函数“wildcard”,它的用法是:$(wildcard PATTER ...

  5. 二进制协议 vs 文本协议

    二进制协议 vs 文本协议 在服务器程序开发过程中,各个服务直接需要进行交互.这样就需要定义消息的协议,一般来说协议主要包括二进制协议和文本协议,下面就我在工作中用到的两种协议说说自己的看法. 1 二 ...

  6. Bootstrap 网格系统(Grid System)的工作原理 - 媒体查询

    媒体查询 媒体查询是非常别致的"有条件的 CSS 规则".它只适用于一些基于某些规定条件的 CSS.如果满足那些条件,则应用相应的样式. Bootstrap 中的媒体查询允许您基于 ...

  7. c++多个文件中如何共用一个全局变量

    例子: 头文件:state.h   源文件:state.cpp 其它源文件:t1.cpp  t2.cpp  t3.cpp, 这些源文件都包含头文件state.h. 需要定义一个全局变量供这些源文件中使 ...

  8. MatLab 2014a编译jar包时mcc无法使用的问题

    http://blog.csdn.net/heroafei/article/details/43273373 MatLab 2014a编译jar包时mcc无法使用的问题 2015-01-29 16:5 ...

  9. Apache+php+mysql环境配置

    Apache+PHP+MySQL环境搭建 标题格式 正文格式 阶段性完成格式 正文中强调格式 ————————————————————————————— 前语:本文是从我写过的doc文档迁移过来的,由 ...

  10. 更换mysql数据库的datadir目录

    更换过程如下: 1.新建指定的datadir目录,这里举例如:E:/mysql_datadir/data. 2.关闭mysql57服务器. 3.将原来的datadir目录下面的所有文件拷贝到E:/my ...