コード例 #1
0
    void Update()
    {
        if (Input.GetButton("Fire1"))   // Fire1按键是鼠标左键或左Ctrl键
        {
            Ray        ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            // layerMask参数使这个射线只能打中指定layer的物体
            if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
            {
                if (hit.transform.gameObject.layer == 8)
                {                                       // 通过hit获取到了击中物体所在的层
                    UFOController UFOCtrl = hit.transform.GetComponent <UFOScript>().ctrl;
                    sceneController.UFOIsShot(UFOCtrl); // 通知sceneController
                }
                else if (hit.transform.gameObject.layer == 9)
                {
                    sceneController.GroundIsShot(hit.point);
                }
            }
            if (muzzleFlashEnable == false) // 显示枪口火焰
            {
                muzzleFlashEnable = true;
                muzzleFlash.SetActive(true);
            }
        }


        if (muzzleFlashEnable)      // 计时,枪口火焰显示0.1秒后消失
        {
            muzzleFlashTimer += Time.deltaTime;
            if (muzzleFlashTimer >= muzzleFlashMaxTime)
            {
                muzzleFlashEnable = false;
                muzzleFlash.SetActive(false);
                muzzleFlashTimer = 0;
            }
        }
    }