TakeDamage() public method

public TakeDamage ( float amount ) : void
amount float
return void
コード例 #1
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.layer == 8)
     {
         BaseStats collisionStats = collision.GetComponent <BaseStats>();
         if (collisionStats.photonView.IsMine)
         {
             Vector2 localKnockbackDir = (transform.right * knockbackDir.x) + (transform.up * knockbackDir.y);
             collisionStats.TakeDamage(dmg, localKnockbackDir, false);
             audioSource.clip = collisionStats.damageTaken[Random.Range(0, collisionStats.damageTaken.Length)];
             audioSource.Play();
         }
     }
 }
コード例 #2
0
    void FixedUpdate()
    {
        transform.position = Vector3.Lerp(transform.position, map.transform.GetChild(currentIndex).transform.position,
                                          Time.fixedDeltaTime * (Time.time - timeOfSlow > slowDuration ? speed : speed * slow) /
                                          Vector3.Distance(transform.position, map.transform.GetChild(currentIndex).transform.position));

        if (Vector3.Distance(transform.position, map.transform.GetChild(currentIndex).transform.position) < errorRange)
        {
            currentIndex++;

            if (currentIndex >= map.transform.childCount)
            {
                baseStats.TakeDamage(damage);
                Destroy(gameObject);
            }
            else
            {
                transform.rotation = Quaternion.Euler(0, 0, Mathf.Rad2Deg *
                                                      Mathf.Atan2(map.transform.GetChild(currentIndex).transform.position.y - transform.position.y,
                                                                  map.transform.GetChild(currentIndex).transform.position.x - transform.position.x) - 90);
            }
        }
    }
コード例 #3
0
    // Update is called once per frame
    void Update()
    {
        target          = GameObject.Find("Player(Clone)");
        targetTransform = target.transform;
        targetStats     = targetTransform.GetComponent <CombatScript>().stats;
        Vector3 healthPos = Camera.main.WorldToScreenPoint(this.transform.position);

        healthPos.y += 40;
        enemyHealthLabel.transform.position = healthPos;
        enemyHealthLabel.text = stats.health + " / " + stats.maxHealth;

        float distance = Vector3.Distance(targetTransform.position, transform.position);

        attackTimer += Time.deltaTime;

        agent.speed = 3.5f;
        if (distance <= lookRadius)
        {
            agent.speed = 6f;
            if (agent.remainingDistance <= agent.stoppingDistance && !agent.pathPending && attackTimer >= 3f)  //TODO: Change based on stats?
            {
                FindObjectOfType <AudioManager>().Play("TakeDamage");
                targetStats.TakeDamage(stats.strength); //TODO: add weapon damage
                attackTimer = 0f;
                Debug.Log("attack");
            }
            else
            {
                agent.SetDestination(targetTransform.position);
                FaceTarget();
            }
        }
        else
        {
            //patrolPos.x += patrolDir * Time.deltaTime * 100;
            //agent.SetDestination(patrolPos);
            //Debug.Log("" + patrolPos.x);
            //TODO: Change destination positions;
            //Debug.Log("Patrol");
            //Debug.Log(agent.remainingDistance.ToString());
            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                if (patrolPos.x == startingPos.x - 5)
                {
                    patrolPos.x = startingPos.x + 25;
                }
                else
                {
                    patrolPos.x = startingPos.x - 5;
                }
                agent.SetDestination(patrolPos);
            }

            /**
             * if (transform.position.x >= startingPos.x + 20)
             * {
             *  Debug.Log("Go Home");
             *  Vector3 newDir = startingPos;
             *  newDir.x -= 5;
             *  agent.SetDestination(newDir);
             * } else if (transform.position.x <= startingPos.x)
             * {
             *  Debug.Log("Go Patrol");
             *  Vector3 newDir = startingPos;
             *  newDir.x +=25;
             *  agent.SetDestination(newDir);
             * }
             **/
        }

        if (stats.health <= 0)
        {
            Destroy(gameObject);
        }
    }
コード例 #4
0
ファイル: PlayerCollision.cs プロジェクト: Eryck13/CandyCrypt
    // Checks with collision between different items.
    private void OnTriggerEnter2D(Collider2D other)
    {
        // Moves player down
        if (other.tag == "Door1")
        {
            this.gameObject.transform.position += new Vector3(0, -upPlayer, 0);
            Down      = true;
            finalPos += new Vector3(0, -upCamera, 0);
        }
        // Moves player up
        else if (other.tag == "Door2")
        {
            this.gameObject.transform.position += new Vector3(0, upPlayer, 0);
            Up        = true;
            finalPos += new Vector3(0, upCamera, 0);
        }
        // Moves player left
        else if (other.tag == "Door3")
        {
            this.gameObject.transform.position += new Vector3(-sidePlayer, 0, 0);
            Left      = true;
            finalPos += new Vector3(-sideCamera, 0, 0);
        }
        // Moves player right
        else if (other.tag == "Door4")
        {
            this.gameObject.transform.position += new Vector3(sidePlayer, 0, 0);
            Right     = true;
            finalPos += new Vector3(sideCamera, 0, 0);
        }
        // Resets level
        else if (other.tag == "StairDown")
        {
            stairDown = true;
            StartCoroutine(Fade1());
            FadeAway.gameObject.SetActive(true);
        }
        // Checks Collision with Enemy
        else if ((other.tag == "Enemy" || other.tag == "shot") && CanBeHit)
        {
            Animation.SetTrigger("Hit");
            AudioManager.instance.Play("PlayerHurt");
            StartCoroutine(hurtTimer());
            playerHitAnim.gameObject.SetActive(true);

            //calls the coroutine function
            StartCoroutine(cooldown());

            //distance the player will travel
            var mag = 1250;
            //calculating the distance between both the player and enemy
            var force = transform.position - other.transform.position;
            //round up the force value
            force.Normalize();
            //pushes the player back
            gameObject.GetComponent <Rigidbody2D>().AddForce(force * mag);
            var healthUI = GetComponent <HealthUI>();
            //takes off a health value on the UI
            stats.TakeDamage(1);

            if (stats.GetTotalModdedHealth() <= 0)
            {
                //so we dont delete the player
                AudioManager.instance.Play("Die");
                this.transform.position = new Vector3(1000f, 1000f, 1000f);
                playerHitAnim.SetActive(false);
            }
        }

        else if (other.tag == "Mud")
        {
            if (!isSlowed)
            {
                isSlowed = true;
            }
        }
    }