unity射击游戏:超萌射手(2)射击特效和EasyButton使用

来源:互联网 发布:怎样装修淘宝店铺视频 编辑:程序博客网 时间:2024/05/02 01:22
本文由作者@zx一路飞奔出品,转载请注明出处
文章地址:http://blog.csdn.net/u014735301/article/details/42705443
作者微博:http://weibo.com/u/1847349851


射击特效



(1)在枪口位置,添加一个点光源 当开枪时。启用点光源。造成一种一闪一闪的效果




(2)添加一个粒子来充当子弹射击的效果,当开枪时,发射粒子




(3)添加一个LineRenderer 线渲染器来模仿射击轨迹,并设置合适的粗细



(4)射击声音也不能少~~~ 


最终效果如下





添加EasyButton






跟joystick属性差不多,这里选择交互方式为Event事件,修改 button纹理。和位置


脚本设置


[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class PlayerShoot : MonoBehaviour {  
  5.   
  6.     //子弹射击频率  
  7.     public float shootRate = 2;  
  8.     public float attack = 30;  
  9.     private float timer = 0;  
  10.     private ParticleSystem particleSystem;  
  11.     private LineRenderer lineRenderer;  
  12.   
  13.   
  14.     // Use this for initialization  
  15.     void Start () {  
  16.         particleSystem = this.GetComponentInChildren<ParticleSystem>();  
  17.         lineRenderer = this.renderer as LineRenderer;  
  18.     }  
  19.   
  20.     void OnEnable()  
  21.     {  
  22.         //按住button  
  23.         EasyButton.On_ButtonPress += On_ButtonPress;  
  24.         //松开button  
  25.         //EasyButton.On_ButtonUp += On_ButtonUp;  
  26.     }  
  27.     void On_ButtonPress(string buttonName)  
  28.     {     
  29.         //如果按下的是fire Button  
  30.         if (buttonName == "Fire")  
  31.         {  
  32.             timer += Time.deltaTime;  
  33.             if (timer > 1 / shootRate)  
  34.             {  
  35.                 timer -= 1 / shootRate;  
  36.                 Shoot();  
  37.             }  
  38.         }  
  39.     }  
  40.     // Update is called once per frame  
  41.     void Update () {  
  42.           
  43.     }  
  44.     //开火  
  45.     void Shoot() {  
  46.         light.enabled = true;  
  47.         particleSystem.Play();  
  48.         this.lineRenderer.enabled = true;  
  49.         lineRenderer.SetPosition(0, transform.position);  
  50.         //----------判断射击到敌人时的游戏逻辑-----------  
  51.         Ray ray = new Ray(transform.position, transform.forward);  
  52.         RaycastHit hitInfo;  
  53.         if (Physics.Raycast(ray, out hitInfo)) {  
  54.             lineRenderer.SetPosition(1, hitInfo.point);  
  55.             //判断当前的射击有没有碰撞到敌人  
  56.             if (hitInfo.collider.tag == Tags.enemy) {  
  57.                 hitInfo.collider.GetComponent<EnemyHealth>().TakeDamage(attack,hitInfo.point);  
  58.             }  
  59.   
  60.         } else {  
  61.             lineRenderer.SetPosition(1, transform.position + transform.forward * 100);  
  62.         }  
  63.         //播放射击音效  
  64.         audio.Play();  
  65.   
  66.         Invoke("ClearEffect", 0.05f);  
  67.     }  
  68.   
  69.     void ClearEffect() {  
  70.         light.enabled = false;  
  71.         lineRenderer.enabled = false;  
  72.     }  
  73. }  

点击Button,持续射击

最终效果,按住Button 持续射击 是不是很萌很暴力呢~~~


0 0
原创粉丝点击