예제 #1
0
파일: RayShooting.cs 프로젝트: 58egor/FPS
 void Update()
 {
     crosshair.UpdateCrosshairActive(Radius * 2, Radius * 2, timeout);
     if (Input.GetMouseButton(1) && !isReload)
     {
         if (!isZoomed)
         {
             isZoomed = true;
             animator.SetTrigger("PricelStart");
             animator.SetBool("Pricel", true);
         }
         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, Time.deltaTime * smooth);
         crosshair.DisableOrActive(false);
     }
     else
     {
         crosshair.DisableOrActive(true);
         isZoomed = false;
         animator.SetTrigger("PricelStop");
         animator.SetBool("Pricel", false);
         camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal, Time.deltaTime * smooth);
     }
     if (((Input.GetMouseButton(0) && !singleShoot) || (Input.GetMouseButtonDown(0) && singleShoot)) && curTimeout <= 0 && !isReload)
     {
         animator.SetBool("FireTest", true);
         currentAmmo--;                                //уменшьаем количество пуль
         otdacha(currentAmmo);                         //вызываем функцию отдачи
         Ray ray = camera.ScreenPointToRay(Razbros()); //создаем луч, вызывая функцию,формирующая направление луча
         curTimeout = timeout;
         RaycastHit[] hit;
         hit = Physics.RaycastAll(ray, Mathf.Infinity, layer);//делаем выстрел
         for (int i = 0; i < hit.Length; i++)
         {
             for (int j = i; j < hit.Length; j++)
             {
                 if (hit[i].distance > hit[j].distance)
                 {
                     RaycastHit copy = hit[i];
                     hit[i] = hit[j];
                     hit[j] = copy;
                 }
             }
         }
         Debug.Log("hit lenght:" + hit.Length);
         for (int i = 0; i < hit.Length; i++)
         {
             Debug.Log("hit:" + hit[i].collider.name);
         }
         for (int i = 0; i < hit.Length; i++)
         {
             if (i < targets)
             {
                 Debug.Log("Damage:" + hit[i].collider.name);
                 if (hit[i].collider.gameObject.layer == 9)
                 {
                     Color color = hit[i].collider.GetComponent <EnemyInfo>().Damage(damage);
                     hitmarker.Active(color);
                 }
                 else
                 {
                 }
             }
             else
             {
                 break;
             }
         }
     }
     else
     {
         curTimeout -= Time.deltaTime;
         if (!singleShoot)
         {
             if ((!Input.GetMouseButton(0)) || isReload || currentAmmo <= 0)
             {
                 curShoots = firstInTarget;
                 animator.SetBool("FireTest", false);
                 contr.otdachaY = 0;
                 contr.otdachaX = 0;
                 Debug.Log("stop sh");
             }
         }
         else
         {
             curShoots = firstInTarget;
             animator.SetBool("FireTest", false);
             if (curTimeout <= 0)
             {
                 contr.otdachaY = 0;
                 contr.otdachaX = 0;
                 Debug.Log("stop sh");
             }
         }
         if (curTimeout <= 0)
         {
             CheckAmmo();//функция отвечающая за перезарядку
         }
     }
     if (Input.GetKey(KeyCode.R) && !isReload && !(currentAmmo == maxAmmo))
     {
         animator.SetTrigger("Reload");
         animator.SetBool("FireTest", false);
         Debug.Log("ActiveReload");
         isReload = true;
     }
     text.text = currentAmmo.ToString();
 }