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


    注: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<Detonator>().size = 3;
            //销毁特效
            Destroy(bombTinys, 2.0f);
            //调用Build脚本中的SetTarget()函数
            bombs.GetComponent<Bomb>().SetTarget(TargetMove, BombWide);

            //调用Bomb脚本中的SetTarget()函数
            bombs.GetComponent<Bomb>().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

  • 相关阅读:
    vue-cli + webpack 多页面实例配置优化方法
    Python Web(1):建立第一个Web项目
    C# winform用sharpGL(OpenGl)解析读取3D模型obj
    CSS outline 属性
    sqlserver查询两个值是否相等
    vue v-for(数组遍历)
    内存查看工具RAMMAP说明
    linux 入门
    linux 内核根文件系统
    linux 命令
  • 原文地址:https://www.cnblogs.com/Cocomo/p/5642662.html
Copyright © 2020-2023  润新知