新动画系统:
反向动力学动画(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. listview点击控件显示EditText,键盘弹出消失的解决方法:

    1.软键盘弹出后消失解决方法 AndoridManifet 在activity中添加: android:windowSoftInputMode="adjustPan" 2.使用方式 ...

  2. 2017php经典面试题

    1.PHP语言的一大优势是跨平台,什么是跨平台?一.PHP基础: PHP的运行环境最优搭配为Apache+MySQL+PHP,此运行环境可以在不同操作系统(例如windows.Linux等)上配置,不 ...

  3. [py]python中的特殊类class type和类的两面性图解

    生活中的模具 生活中 编程 万物都从无到有, 起于烟尘 () 生产原料,铁 object 车床-生产各类模具 元类即metaclass,对应python的class type 模具-生产各类实在的物品 ...

  4. 主成分分析(PCA)算法,K-L变换 角度

    主成分分析(PCA)是多元统计分析中用来分析数据的一种方法,它是用一种较少数 量的特征对样本进行描述以达到降低特征空间维数的方法,它的本质实际上是K-L变换.PCA方法最著名的应用应该是在人脸识别中特 ...

  5. python三步实现人脸识别

    原文地址https://www.toutiao.com/a6475797999176417550 Face Recognition软件包 这是世界上最简单的人脸识别库了.你可以通过Python引用或者 ...

  6. 实体框架 Code First

    原文:https://msdn.microsoft.com/zh-cn/en-zn/data/jj591621

  7. qt用mingw编译时报错 multiple definition of

    网上相关回答不少,但过于简单,这里做一下记录. qt用mingw编译程序时报“multiple definition of …”这个错误,错误信息大概是如下图所示: 1 2 3 首先,检查自己的程序是 ...

  8. 读书--编写高质量代码 改善C#程序的157个建议2

    重新从图书馆将这本书借出来,看一遍似乎记不住,这次打算看一点就记录点,记录下自己容易忘记的知识点,便于记住. 建议1:正确使用字符串: 1    string str1= "hellowor ...

  9. django登录功能(简单在POST请求)

    第一  先在templates中创立index.html !DOCTYPE html> <head> <meta charset="UTF-8"> & ...

  10. VS2010/MFC编程入门之三十三(常用控件:标签控件Tab Control 下)

    上一节中鸡啄米讲了标签控件知识的上半部分,本节继续讲下半部分. 标签控件的创建 MFC为标签控件的操作提供了CTabCtrl类. 与之前的控件类似,创建标签控件可以在对话框模板中直接拖入Tab Con ...