1. 程式人生 > >Unity運動殘影技能

Unity運動殘影技能

維護 out rec src sta while 重要 view particle

  殘影實現:

    1、List<DrawMesh> list,此list中包含某一幀動畫模型網格、材質

    2、每過一段時間就將運動物體的模型add到list中(優化:未實現,網格合並)

    3、LateUpdate裏將list中所有模型材質alpha減少,為0,則remove;其余的模型 DrawMesh

  技能實現:

    技能為兩部分,前一部分是運動殘影,後一部分是落地爆炸,協程處理即可。

ps

 此例簡單實現了這個技能,不過應該能夠做的更好一些,就是給運動物體添加狀態:空中、即將落地,需要維護這兩個狀態,並且需要用射線處理將爆炸粒子放在合適的位置play

代碼:

技術分享
  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 
  5 class DrawMesh
  6 {
  7     public Mesh mesh;
  8     public Matrix4x4 matrix;
  9     public Material material;
 10     public DrawMesh(Mesh me, Matrix4x4 ma, Material mat, Color color)
 11     {
12 this.mesh = me; 13 this.matrix = ma; 14 this.material = new Material(mat); 15 this.material.shader = Shader.Find("Transparent/VertexLit"); 16 this.material.SetColor("_Emission", color); 17 } 18 } 19 20 public class Skill4 : MonoBehaviour 21 { 22
private SkinnedMeshRenderer[] render; 23 24 25 public float forwardPower;//移動速度 26 public float jumpPower;//跳躍速度 27 private bool isStart;//殘影是否開始 28 29 public float Skill4ContinueTime;//殘影過程總時間 30 private float time; 31 public float fadeSpeed;//殘影漸變消失速率 32 33 public float rate;//繪制殘影速率 34 public Color color;//殘影顏色 35 private List<DrawMesh> list;//殘影list 36 37 public ParticleSystem particle;//結束爆炸粒子 38 private Vector3 particlePosition;//粒子初始位置,粒子掛在玩家身上 39 40 41 void Awake() 42 { 43 render = GetComponentsInChildren<SkinnedMeshRenderer>(); 44 list = new List<DrawMesh>(); 45 isStart = false; 46 time = 0; 47 particlePosition = particle.transform.localPosition; 48 } 49 50 void Update() 51 { 52 if (Input.GetKeyDown(KeyCode.H)) 53 { 54 UseSkill(); 55 } 56 } 57 58 public void UseSkill() 59 { 60 playerCS.CrossFade("Attack3Anim", 0f); 61 isStart = true; 62 time = 0; 63 StartCoroutine(IEStartSkill()); 64 } 65 66 private IEnumerator IEStartSkill() 67 { 68 rigidbody.velocity = rigidbody.velocity + Vector3.up * jumpPower;//跳躍速度 69 while (isStart) 70 { 71 for (int i = 0; i < render.Length; i++) 72 { 73 Mesh mesh = new Mesh(); 74 render[i].BakeMesh(mesh); 75 list.Add(new DrawMesh(mesh, render[i].transform.localToWorldMatrix, render[i].material, color)); 76 } 77 yield return new WaitForSeconds(rate); 78 } 79 } 80 81 void FixedUpdate() 82 { 83 if (isStart) 84 { 85 if (time < Skill4ContinueTime)//殘影過程中 86 { 87 time += Time.deltaTime; 88 rigidbody.velocity = transform.TransformDirection(Vector3.forward * forwardPower) + Vector3.up * rigidbody.velocity.y; 89 } 90 else//殘影過程結束 進入爆炸過程 91 { 92 isStart = false; 93 rigidbody.velocity = Vector3.up*rigidbody.velocity.y; 94 playerCS.CrossFade("IdleAnim", 1f); 95 StartCoroutine(IEBoom()); 96 } 97 } 98 } 99 100 IEnumerator IEBoom() 101 { 102 particle.transform.parent = null; 103 104 particle.gameObject.SetActive(true); 105 particle.Play(); 106 yield return new WaitForSeconds(particle.duration); //爆炸過程結束 107 particle.gameObject.SetActive(false); 108 particle.transform.parent = transform; 109 particle.transform.localPosition = particlePosition; 110 } 111 112 void LateUpdate() 113 { 114 for (int i = list.Count - 1; i >= 0; i--) 115 { 116 list[i].material.SetColor("_Color", new Color(color.r, color.g, color.b, list[i].material.color.a - Time.deltaTime * fadeSpeed)); 117 if (list[i].material.color.a <= 0.05f) 118 { 119 Destroy(list[i].material);//重要,如果不destroy顯存占用越來越多 120 Destroy(list[i].mesh); 121 list.RemoveAt(i); 122 } 123 } 124 for (int i = list.Count - 1; i >= 0; i--) 125 { 126 Graphics.DrawMesh(list[i].mesh, list[i].matrix, list[i].material, gameObject.layer); 127 } 128 } 129 }
View Code

效果:

技術分享

Unity運動殘影技能