Ejemplo n.º 1
0
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Lander")
     {
         shipHealth.ReduceHealth();
         screenShake.AddTrauma(1.0f);
         Destroy(gameObject);
     }
 }
Ejemplo n.º 2
0
    void OnTriggerEnter2D(Collider2D other)
    {
        Debug.Log(other);
        var asteroid = other.GetComponent <Asteroid>();

        if (asteroid)
        {
            screenShake.AddTrauma(0.5f);

            asteroid.SpawnCross();
            Destroy(asteroid.gameObject);

            chunksSystem.Play();
            Destroy(gameObject);
        }
    }
Ejemplo n.º 3
0
    void Update()
    {
        if (stopTilt)
        {
            return;
        }

        float shipAngleOffset = Mathf.PerlinNoise(randomSeed, Time.time);

        //scale to (-1;1)
        shipAngleOffset  = shipAngleOffset * 2f - 1f;
        shipAngleOffset *= noiseStrength;

        shipAngle += shipAngleOffset * Time.deltaTime;
        shipAngle  = Mathf.Clamp(shipAngle, -maxShipAngle, maxShipAngle);


        outsideCamera.rotation = Quaternion.Euler(0f, 0f, shipAngle * 15f);

        bool angleInWarningRange = shipAngle > shipAngleToHealthDrop || shipAngle < -shipAngleToHealthDrop;

        if (healthDropping)
        {
            if (!angleInWarningRange)
            {
                warning.EndWarning();
                healthDropping  = false;
                healthDropTimer = 0f;
            }

            healthDropTimer += Time.deltaTime;
            if (healthDropTimer > timeUntilHealthDrop)
            {
                health.ReduceHealth();
                screenShake.AddTrauma(0.5f);
                shipAngle       = 0f;
                healthDropTimer = 0f;
            }
        }
        else if (angleInWarningRange)
        {
            healthDropping = true;
            warning.StartWarning();
        }
    }
Ejemplo n.º 4
0
    void DoRangedAttack()
    {
        if (shotsLeft <= 0)
        {
            return;
        }

        List <Vector3> linePositionsInitial = new List <Vector3>();
        List <Vector3> linePositionsBounced = new List <Vector3>();

        linePositionsInitial.Add(transform.position);

        nextRay.origin    = transform.position;
        nextRay.direction = (input.LookAtPos - transform.position).normalized;

        int  rangedHitCount;
        bool wallBounce = isPiercing;

        float coneDot   = Mathf.Cos(afterBounceHelpAngle * Mathf.Deg2Rad);
        float helpAngle = afterBounceHelpAngle * Mathf.Deg2Rad;

        for (int i = 0; i < bounceLimit; i++)
        {
            bool firstRay = i == 0;

            var linePositions = firstRay ? linePositionsInitial : linePositionsBounced;
            rangedHitCount = 0;

            RaycastHit finalHit;
            //Shoot simple ray and check for walls/shields
            rangedHitCount = Physics.Raycast(nextRay, out finalHit, Mathf.Infinity, rayMask) ? 1 : 0;
            Debug.Log(i + " : " + rangedHitCount);
            Debug.Log(finalHit.point);

            //If it is a bounced shot, do the cone check and realign based on the least different angle enemy found
            if (!firstRay && helperOnDebug)
            {
                //Calculate optimal spehere Cast radius
                float hitToSurfaceAngle    = (90f + Vector3.Angle(finalHit.normal, -nextRay.direction)) * Mathf.Deg2Rad;
                float maxDistanceToSurface = finalHit.distance * Mathf.Sin(hitToSurfaceAngle) / Mathf.Sin(Mathf.PI - hitToSurfaceAngle - helpAngle);
                float sphereCastRadius     = maxDistanceToSurface * Mathf.Sin(helpAngle) / Mathf.Sin(Mathf.PI / 2f - helpAngle);

                //Backtrack ray origin by radius to hit avoid enemies being inside the initial sphere
                Debug.Log(nextRay.origin.ToString() + nextRay.direction.ToString() + sphereCastRadius.ToString());
                rangedHitCount = Physics.SphereCastNonAlloc(nextRay.origin - nextRay.direction * sphereCastRadius * 2f, sphereCastRadius, nextRay.direction, rangedHits, Mathf.Infinity, enemyLayerMask);

                //For each enemy, check if theyre actually in the cone by comparing dot product
                float bestCaseAngle = -1f;
                for (int j = 0; j < rangedHitCount; j++)
                {
                    Debug.Log("enemy hit on: " + rangedHits[j].collider.gameObject.name);
                    var dirToEnemy = (rangedHits[j].point - nextRay.origin).normalized;
                    var enemyDot   = Vector3.Dot(nextRay.direction, dirToEnemy);
                    if (!firstRay && enemyDot < coneDot)
                    {
                        //Enemy is not within the cone
                        continue;
                    }
                    if (enemyDot > bestCaseAngle)
                    {
                        bestCaseAngle     = enemyDot;
                        nextRay.direction = dirToEnemy;
                    }
                }
                //New final hit
                if (bestCaseAngle > -1f)
                {
                    rangedHitCount = Physics.Raycast(nextRay, out finalHit, Mathf.Infinity, rayMask) ? 1 : 0;
                }
            }

            //Sphere cast for enemiessa
            if (isPiercing)
            {
                rangedHitCount = Physics.SphereCastNonAlloc(nextRay, initialSpherecastRadius, rangedHits, finalHit.distance, enemyLayerMask);
            }
            else
            {
                rangedHitCount = Physics.SphereCast(nextRay, initialSpherecastRadius, out rangedHits[0], finalHit.distance, enemyLayerMask) ? 1 : 0;
            }

            //For each enemy, reduce health
            for (int j = 0; j < rangedHitCount; j++)
            {
                var health = rangedHits[j].collider.GetComponent <HealthSystem>();
                if (!firstRay && health)
                {
                    health.Kill();
                    if (!isPiercing)
                    {
                        finalHit = rangedHits[j];
                    }

                    var hitEffectObj = Instantiate(hitEffect, rangedHits[j].point, Quaternion.identity);
                    hitEffectObj.transform.LookAt(rangedHits[j].point + rangedHits[j].normal);
                }
            }
            //Do bounce if needed
            Debug.Log(finalHit.point);
            if (shieldLayerMask == (shieldLayerMask | (1 << finalHit.collider.gameObject.layer)))
            {
                BounceShot(finalHit, ref linePositions);
            }
            else if (wallBounce && (wallLayerMask == (wallLayerMask | (1 << finalHit.collider.gameObject.layer))))
            {
                wallBounce = false;
                BounceShot(finalHit, ref linePositions);
            }
            else
            {
                linePositions.Add(finalHit.point);
                var hitEffectObj = Instantiate(hitEffect, finalHit.point, Quaternion.identity);
                hitEffectObj.transform.LookAt(finalHit.point + finalHit.normal);
                hasBounced = false;
            }

            if (!hasBounced)
            {
                break;
            }
        }

        //Populate effect data
        var bulletTrail = isPiercing ? Instantiate(bulletTrailPrefabPiercing) : Instantiate(bulletTrailPrefab);

        bulletTrail.SetTransform(transform);
        linePositionsBounced.Insert(0, linePositionsInitial[linePositionsInitial.Count - 1]);
        bulletTrail.SetPositionsInitial(linePositionsInitial.ToArray());
        bulletTrail.SetPositionsAfterBounce(linePositionsBounced.ToArray());

        //Screenshake
        float trauma = isPiercing ? screenShakeTraumaPerPiercingShot : screenShakeTraumaPerShot;

        screenShake.AddTrauma(trauma);

        //Hitstop

        //Decrease ammo and reset effects/charging
        shotsLeft--;
        isPiercing          = false;
        chargeTimer         = 0.0f;
        chargeEffectStarted = false;
        chargeEffect.SetActive(false);
        aimingEffect.SetActive(false);
    }