Unity3D学习笔记(十七):IK动画、粒子系统和塔防
设置头、手、肘的目标点

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构成

可以通过SizeOverLife曲线

也可以选择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只负责自身

取消唤醒,改为代码播放

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++;
}
}
}
}
保留原动画状态机的逻辑,可以替换原动画片段

内层添加球形碰撞体,勾选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);
}
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动画、粒子系统和塔防的更多相关文章
- python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容
python3.4学习笔记(十七) 网络爬虫使用Beautifulsoup4抓取内容 Beautiful Soup 是用Python写的一个HTML/XML的解析器,它可以很好的处理不规范标记并生成剖 ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
- unity3d学习笔记(一) 第一人称视角实现和倒计时实现
unity3d学习笔记(一) 第一人称视角实现和倒计时实现 1. 第一人称视角 (1)让mainCamera和player(视角对象)同步在一起 因为我们的player是生成的,所以不能把mainCa ...
- Unity3D学习笔记2——绘制一个带纹理的面
目录 1. 概述 2. 详论 2.1. 网格(Mesh) 2.1.1. 顶点 2.1.2. 顶点索引 2.2. 材质(Material) 2.2.1. 创建材质 2.2.2. 使用材质 2.3. 光照 ...
- 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. 渲染 ...
- Unity3D学习笔记4——创建Mesh高级接口
目录 1. 概述 2. 详论 3. 其他 4. 参考 1. 概述 在文章Unity3D学习笔记2--绘制一个带纹理的面中使用代码的方式创建了一个Mesh,不过这套接口在Unity中被称为简单接口.与其 ...
- Unity3D学习笔记6——GPU实例化(1)
目录 1. 概述 2. 详论 3. 参考 1. 概述 在之前的文章中说到,一种材质对应一次绘制调用的指令.即使是这种情况,两个三维物体使用同一种材质,但它们使用的材质参数不一样,那么最终仍然会造成两次 ...
- Unity3D学习笔记7——GPU实例化(2)
目录 1. 概述 2. 详论 2.1. 实现 2.2. 解析 3. 参考 1. 概述 在上一篇文章<Unity3D学习笔记6--GPU实例化(1)>详细介绍了Unity3d中GPU实例化的 ...
- Unity3D学习笔记8——GPU实例化(3)
目录 1. 概述 2. 详论 2.1. 自动实例化 2.2. MaterialPropertyBlock 3. 参考 1. 概述 在前两篇文章<Unity3D学习笔记6--GPU实例化(1)&g ...
- Unity3D学习笔记12——渲染纹理
目录 1. 概述 2. 详论 3. 问题 1. 概述 在文章<Unity3D学习笔记11--后处理>中论述了后处理是帧缓存(Framebuffer)技术实现之一:而另外一个帧缓存技术实现就 ...
随机推荐
- js-jquery-对象与JSON字符串互相转换
1:jQuery插件支持的转换方式 代码如下: String→Object$.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转 ...
- git 生成patch 和打入patch
转载:https://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用gi ...
- (转)Elasticsearch查询规则------match和term
es种有两种查询模式,一种是像传递URL参数一样去传递查询语句,被称为简单搜索或查询字符串(query string)搜索,比如 GET /megacorp/employee/_search //查询 ...
- PAT 1049 Counting Ones[dp][难]
1049 Counting Ones (30)(30 分) The task is simple: given any positive integer N, you are supposed to ...
- sklearn总览
- JS中的对象数组
<html> <head> <title>对象数组的字符串表示</title> <script type="text/javascrip ...
- VCS中的覆盖率分析
VCS在仿真过程中,也可以收集Coverage Metric.其中覆盖率类型有: 1)Code Coverage:包括control_flow和value两部分的coverage,line_cover ...
- NC_Verilog中的工具ICC
Cadence中的Incisive Comprehensive Coverage(ICC) solusion提供在仿真中的覆盖率分析. ICC中的覆盖率类型有两大类: 1)Code Coverage: ...
- 植物大战僵尸作弊器源代码(MFC版)
控制版使用不太方便,此MFC版与控制台版内容一样.具体可以参考前面.此处只附源代码,不加以说明.......... 头文件 // jsMFCDlg.h : 头文件 // #pragma once // ...
- AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) Chapter 3. Data Structures Fundamental Data Structures
10410 这题说的是给了一棵多叉树的 bfs遍历轨迹 和dfs 遍历 轨迹 在父节点向叶子节点扩展的时候优先遍历编号较小的节点.我还原出这课树特殊判定 根据bfs的顺序来建立这课树,用一个队列安排要 ...