Esempio n. 1
0
    void Shoot()
    {
        for (int i = 0; i < firesKeys.Length; i++)
        {
            if (Input.GetButtonDown(firesKeys[i]) && cooldowns[i] <= 0)
            {
                GameObject  shot        = Instantiate(firesPrefabs[i], firesPrefabs[i].transform.position + transform.position, firesPrefabs[i].transform.rotation);
                ShotHandler shotHandler = shot.GetComponent <ShotHandler>();
                shotHandler.setColorToChangeOn(transform.GetChild(0).GetComponent <Renderer>().material.color);

                cooldowns[i] = delays[i];
                break;
            }
        }

        for (int i = 0; i < firesKeys.Length; i++)
        {
            cooldowns[i] -= Time.deltaTime;
            string textOnCounter = "";

            if (cooldowns[i] < 0)
            {
                textOnCounter = "0.0";
            }
            else
            {
                string cooldownText = cooldowns[i].ToString();
                textOnCounter = cooldownText.Substring(0, cooldownText.IndexOf(".") + 2);
            }

            thisPlayersCooldownTexts[i].text = textOnCounter;
        }
    }
Esempio n. 2
0
    void Shoot()
    {
        if (buttonsValues[1] == 1 && cooldowns[0] <= 0)
        {
            Vector3    pos  = new Vector3(transform.position.x, firesPrefabs[0].transform.position.y, transform.position.z);
            GameObject shot = Instantiate(firesPrefabs[0], pos, firesPrefabs[0].transform.rotation, bulletsTransform) as GameObject;
            shot.GetComponent <MeshRenderer>().material.color = transform.GetChild(0).GetComponent <Renderer>().material.color;
            cooldowns[0] = delays[0];
        }

        if (buttonsValues[2] == 1 && cooldowns[1] <= 0 && canFireSuperShot)
        {
            Vector3    pos  = new Vector3(transform.position.x, firesPrefabs[3].transform.position.y, transform.position.z);
            GameObject shot = Instantiate(firesPrefabs[3], pos, firesPrefabs[3].transform.rotation, bulletsTransform) as GameObject;
            shot.GetComponent <MeshRenderer>().material.color = transform.GetChild(0).GetComponent <Renderer>().material.color;
            cooldowns[1] = delays[3];

            canFireSuperShot = false;
        }
        else if (buttonsValues[2] == 1 && cooldowns[1] <= 0 && !canFireSuperShot)
        {
            float posX = 0;
            if (transform.position.x > 0)
            {
                posX = transform.position.x - 8;
            }
            else
            {
                posX = transform.position.x + 8;
            }

            Vector3    pos  = new Vector3(posX, firesPrefabs[1].transform.position.y, transform.position.z);
            GameObject wall = Instantiate(firesPrefabs[1], pos, firesPrefabs[1].transform.rotation, bulletsTransform) as GameObject;
            wall.GetComponent <MeshRenderer>().material.color = transform.GetChild(0).GetComponent <Renderer>().material.color;
            wall.GetComponent <FrostWall>().setWhoCreated(gameObject);
            cooldowns[1] = delays[1];
        }

        if (buttonsValues[3] == 1 && cooldowns[2] <= 0)
        {
            Vector3     pos         = new Vector3(transform.position.x, firesPrefabs[2].transform.position.y, transform.position.z);
            GameObject  shot        = Instantiate(firesPrefabs[2], pos, firesPrefabs[2].transform.rotation, bulletsTransform) as GameObject;
            ShotHandler shotHandler = shot.GetComponent <ShotHandler>();
            shot.GetComponent <MeshRenderer>().material.color = transform.GetChild(0).GetComponent <Renderer>().material.color;
            cooldowns[2] = delays[2];
        }
    }
Esempio n. 3
0
    void Update()
    {
        if (lineRenderer != null && Time.time > lastShootTime + 0.1f)
        {
            lineRenderer.SetPositions(noline);
        }

        //Skyter på den nærmeste duden
        target = unit.closestEnemy;

        //om vi har et mål
        if (target != null)
        {
            //Retningen mot målet
            Vector3 targetDirection = target.transform.position - weapon.barrelEnd.position;
            //Fetningen mot målet om man ignorerer høyde
            Vector3 flatDirection = targetDirection - Vector3.up * targetDirection.y;

            //Roterer mounten
            Quaternion mountRotation = Quaternion.LookRotation(flatDirection, Vector3.up);
            mount.rotation = Quaternion.RotateTowards(mount.rotation, mountRotation, mountRotationSpeed * Time.deltaTime);

            //Roterer våpenet
            float angle = Mathf.Atan2(targetDirection.y, new Vector2(flatDirection.x, flatDirection.z).magnitude) * Mathf.Rad2Deg;
            weapon.transform.localRotation = Quaternion.RotateTowards(weapon.transform.localRotation, Quaternion.Euler(new Vector3(-angle, 0, 0)), weaponElevationSpeed * Time.deltaTime);

            //Om mounten er riktig rotert
            if (Quaternion.Angle(mount.rotation, mountRotation) < 1)
            {
                //Om våpenet er riktig rotert
                if (Quaternion.Angle(weapon.transform.localRotation, Quaternion.Euler(new Vector3(-angle, 0, 0))) < 1)
                {
                    //Oppdaterer retningen til fra tuppen av våpenet
                    targetDirection = target.transform.position - weapon.barrelEnd.position;
                    if (Time.time >= lastShootTime + (60f / weapon.firerate))
                    {
                        //Ser hva vi treffer
                        RaycastHit hit;
                        if (Physics.Raycast(weapon.barrelEnd.position, targetDirection, out hit, weapon.range))
                        {
                            //Om det er en fientlig unit
                            hitUnit = hit.transform.GetComponent <Unit>();
                            if (hitUnit != null && hitUnit.team != unit.team)
                            {
                                //Husker når vi sist skjøt
                                lastShootTime = Time.time;

                                //Om det ikke skyter noe prosjektil
                                if (weapon.ammo.projectilePrefab == null)
                                {
                                    //Accuracy
                                    Vector3 shootDirection = targetDirection.normalized + (new Vector3(Random.value - 0.5f, Random.value - 0.5f, Random.value - 0.5f) * 2 / weapon.accuracy);

                                    //Ser om vi treffer noe
                                    if (Physics.Raycast(weapon.barrelEnd.position, shootDirection, out hit, weapon.range))
                                    {
                                        //Tegner linje
                                        //Debug.DrawLine(weapon.barrelEnd.position, hit.point, Color.yellow, 0.1f);
                                        lineRenderer.SetPositions(new Vector3[2] {
                                            weapon.barrelEnd.position, hit.point
                                        });

                                        //Ser om vi treff en unit
                                        hitUnit = hit.transform.GetComponent <Unit>();
                                        if (hitUnit != null)
                                        {
                                            //Påfører skade
                                            hitUnit.Damage(weapon.ammo);
                                        }
                                    }
                                    //Om vi ikke traff noe
                                    else
                                    {
                                        //Tegner en linje med lengden til våpenets rekkevidde
                                        //Debug.DrawRay(weapon.barrelEnd.position, , Color.yellow, 0.2f);
                                        lineRenderer.SetPositions(new Vector3[2] {
                                            weapon.barrelEnd.position, weapon.barrelEnd.position + shootDirection.normalized * weapon.range
                                        });
                                    }
                                }
                                //Om vi skal skyte et prosjektil
                                else
                                {
                                    //Lager et nytt prosjektil
                                    GameObject newShell = Instantiate <GameObject>(weapon.ammo.projectilePrefab);

                                    //Setter layeret til Default
                                    newShell.layer = 0;
                                    //Posisjonerer det
                                    newShell.transform.position = weapon.barrelEnd.position;
                                    newShell.transform.rotation = weapon.barrelEnd.rotation;

                                    //Henter ShotHandler komponenten
                                    ShotHandler shotHandlerShell = newShell.GetComponent <ShotHandler>();

                                    //Setter farten
                                    newShell.GetComponent <Rigidbody>().velocity = ((target.transform.position - weapon.barrelEnd.position).normalized) * shotHandlerShell.Speed;

                                    //Setter airtime
                                    float range = Mathf.Min((target.transform.position - weapon.barrelEnd.position).magnitude, weapon.range);
                                    shotHandlerShell.AirTime = weapon.ammo.damageType == DamageType.Explosive ? range / shotHandlerShell.Speed + 1: 20;

                                    //Setter hvilken ammo prosjektilet kommer fra
                                    shotHandlerShell.ammo = weapon.ammo;
                                }
                            }
                        }
                    }
                }
            }
        }
    }