private void Update()
    {
        if (_frozen)
        {
            return;
        }

        _timeAlive += Time.deltaTime;

        if (_timeAlive >= TurnController.TURN_DURATION * _turnLifetime)
        {
            Destroy(gameObject);
            return;
        }

        transform.Translate(transform.forward * (_speed * Time.deltaTime), Space.World);

        Vector3 rayDir = transform.position - _previousFramePosition;

        if (Physics.Raycast(_previousFramePosition, rayDir, out RaycastHit rayHit, rayDir.magnitude))
        {
            GameObject hitGO = rayHit.collider.gameObject;
            if (hitGO.layer == 10)             // Entity Layer
            {
                Entity entity = hitGO.GetComponentInParent <Entity>();

                if (entity.id != _sourceId)
                {
                    StatsController stats = entity.GetComponent <StatsController>();

                    if (hitGO.CompareTag("Hull"))
                    {
                        stats.DealDamage(damage);
                        Destroy(Instantiate(_hullHitEffect, transform.position, Quaternion.LookRotation(-transform.forward)), 5f);
                    }
                    else if (hitGO.CompareTag("Shield"))
                    {
                        stats.DealDamage(damage);
                        Destroy(Instantiate(_shieldHitEffect, transform.position, Quaternion.LookRotation(rayHit.normal)), 5f);
                    }

                    Destroy(gameObject);
                }
            }

            return;
        }

        _previousFramePosition = transform.position;
    }
예제 #2
0
 void OnTriggerEnter2D(Collider2D collider)
 {
     if (collider.tag == "Player")
     {
         grounded = false;
         float direction = Mathf.Sign(transform.position.x - collider.transform.position.x);
         rigid.AddForce(new Vector2(100f * direction, 100));
         statsController.DealDamage(collider.GetComponent <StatsController>().strenght);
     }
 }
 void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.tag == "Ground")
     {
         grounded = true;
     }
     if (collision.gameObject.tag == "Enemy")
     {
         grounded = false;
         float direction = Mathf.Sign(transform.position.x - collision.contacts[0].point.x);
         rigid.AddForce(new Vector2(500f * direction, 500));
         statsController.DealDamage(collision.gameObject.GetComponent <StatsController>().strenght);
         healthBar.transform.localScale = new Vector3(Mathf.Max(0, statsController.Hp), 1f, 1f);
     }
 }