炮弹转向鼠标点击方向,随即发射炮弹并产生爆炸特效

您所在的位置:网站首页 炮弹爆炸效果图 炮弹转向鼠标点击方向,随即发射炮弹并产生爆炸特效

炮弹转向鼠标点击方向,随即发射炮弹并产生爆炸特效

2024-05-09 08:12| 来源: 网络整理| 查看: 265

 

注:Paotai为大炮,所挂载的脚本为Move.

using UnityEngine;using System.Collections;

public class Move : MonoBehaviour{  

  public Transform TargetMove;//作为炮口的瞄准目标   private GameObject _gameuseY;

  public GameObject bomb;//炮弹   public GameObject gun;//炮口   //爆炸特效   public GameObject BombTiny;//爆炸小特效   public GameObject BombWide;//爆炸大特效   void Start()   {     //实例化出名字为_gameuseY的参照物体     _gameuseY = new GameObject("_gameuseY");   }   void Update ()   {     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);     RaycastHit hit;     if (Physics.Raycast(ray,out hit,100))     {         TargetMove.position = hit.point;       _gameuseY.transform.LookAt(hit.point);       //炮台沿着_gameuseY的Y轴旋转【旋转eulerAngles】。       transform.eulerAngles = new Vector3(transform.eulerAngles.x,_gameuseY.transform.eulerAngles.y,transform.eulerAngles.z);       if (Input.GetMouseButtonDown(0))       {         //按下鼠标左键         GameObject bombs = Instantiate(bomb) as GameObject;         //炮弹从炮口位置发射         bomb.transform.position = gun.transform.position;         //实例化出炮口位置的爆炸特效         GameObject bombTinys = Instantiate(BombTiny) as GameObject;         //炮弹小特效的父物体是炮口         bombTinys.transform.parent = gun.transform;         //设置爆炸特效的初始位置         bombTinys.transform.localPosition = Vector3.zero;         //调用爆炸特效脚本Detonator的size属性         bombTinys.GetComponent().size = 3;         //销毁特效         Destroy(bombTinys, 2.0f);         //调用Build脚本中的SetTarget()函数         bombs.GetComponent().SetTarget(TargetMove, BombWide);

        //调用Bomb脚本中的SetTarget()函数         bombs.GetComponent().SetTarget(TargetMove, BombWide);     }   }  }}

Bomb脚本

using UnityEngine;using System.Collections;

public class Bomb : MonoBehaviour{   GameObject chun;   Vector3 ve;   public void SetTarget(Transform trang ,GameObject chun)   {     ve = trang.position;     this.chun = chun;//爆炸范围的预设体   }   void Update()   {     if (ve != null)     { //如果炮弹存在       if (Vector3.Distance(transform.position, ve) < 0.01f)       {         //实例化爆炸特效的爆炸范围的预设体         GameObject chunks = Instantiate(chun) as GameObject;         chunks.transform.position = ve;        destroy(gameObject);       }       //爆炸特效朝向发出的炮弹方向移动       gameObject.transform.position = Vector3.MoveTowards(transform.position, ve, 20f * Time.deltaTime);

    }   }  }

效果图:

注:代码参考CannonFinal

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3