Unity运动残影技能

时间:2024-04-10 10:35:46

  残影实现:

    1、List<DrawMesh> list,此list中包含某一帧动画模型网格、材质

    2、每过一段时间就将运动物体的模型add到list中(优化:未实现,网格合并)

    3、LateUpdate里将list中所有模型材质alpha减少,为0,则remove;其余的模型 DrawMesh

  技能实现:

    技能为两部分,前一部分是运动残影,后一部分是落地爆炸,协程处理即可。

ps

 此例简单实现了这个技能,不过应该能够做的更好一些,就是给运动物体添加状态:空中、即将落地,需要维护这两个状态,并且需要用射线处理将爆炸粒子放在合适的位置play

代码:

 using UnityEngine;
using System.Collections;
using System.Collections.Generic; class DrawMesh
{
public Mesh mesh;
public Matrix4x4 matrix;
public Material material;
public DrawMesh(Mesh me, Matrix4x4 ma, Material mat, Color color)
{
this.mesh = me;
this.matrix = ma;
this.material = new Material(mat);
this.material.shader = Shader.Find("Transparent/VertexLit");
this.material.SetColor("_Emission", color);
}
} public class Skill4 : MonoBehaviour
{
private SkinnedMeshRenderer[] render; public float forwardPower;//移动速度
public float jumpPower;//跳跃速度
private bool isStart;//残影是否开始 public float Skill4ContinueTime;//残影过程总时间
private float time;
public float fadeSpeed;//残影渐变消失速率 public float rate;//绘制残影速率
public Color color;//残影颜色
private List<DrawMesh> list;//残影list public ParticleSystem particle;//结束爆炸粒子
private Vector3 particlePosition;//粒子初始位置,粒子挂在玩家身上 void Awake()
{
render = GetComponentsInChildren<SkinnedMeshRenderer>();
list = new List<DrawMesh>();
isStart = false;
time = ;
particlePosition = particle.transform.localPosition;
} void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
UseSkill();
}
} public void UseSkill()
{
playerCS.CrossFade("Attack3Anim", 0f);
isStart = true;
time = ;
StartCoroutine(IEStartSkill());
} private IEnumerator IEStartSkill()
{
rigidbody.velocity = rigidbody.velocity + Vector3.up * jumpPower;//跳跃速度
while (isStart)
{
for (int i = ; i < render.Length; i++)
{
Mesh mesh = new Mesh();
render[i].BakeMesh(mesh);
list.Add(new DrawMesh(mesh, render[i].transform.localToWorldMatrix, render[i].material, color));
}
yield return new WaitForSeconds(rate);
}
} void FixedUpdate()
{
if (isStart)
{
if (time < Skill4ContinueTime)//残影过程中
{
time += Time.deltaTime;
rigidbody.velocity = transform.TransformDirection(Vector3.forward * forwardPower) + Vector3.up * rigidbody.velocity.y;
}
else//残影过程结束 进入爆炸过程
{
isStart = false;
rigidbody.velocity = Vector3.up*rigidbody.velocity.y;
playerCS.CrossFade("IdleAnim", 1f);
StartCoroutine(IEBoom());
}
}
} IEnumerator IEBoom()
{
particle.transform.parent = null; particle.gameObject.SetActive(true);
particle.Play();
yield return new WaitForSeconds(particle.duration); //爆炸过程结束
particle.gameObject.SetActive(false);
particle.transform.parent = transform;
particle.transform.localPosition = particlePosition;
} void LateUpdate()
{
for (int i = list.Count - ; i >= ; i--)
{
list[i].material.SetColor("_Color", new Color(color.r, color.g, color.b, list[i].material.color.a - Time.deltaTime * fadeSpeed));
if (list[i].material.color.a <= 0.05f)
{
Destroy(list[i].material);//重要,如果不destroy显存占用越来越多
Destroy(list[i].mesh);
list.RemoveAt(i);
}
}
for (int i = list.Count - ; i >= ; i--)
{
Graphics.DrawMesh(list[i].mesh, list[i].matrix, list[i].material, gameObject.layer);
}
}
}

效果:

Unity运动残影技能