Ejemplo n.º 1
0
 void OnCollisionEnter(Collision collision)
 {
     //collision.gameObject is the player
     if (collision.gameObject.tag == "Player" && weapon.getDurability() > 0)
     {
         equipWeapon();
     }
 }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (equippedWeapon != null)
        {
            if (GameObject.Find("WeaponInfo") != null)
            {
                weaponInfo       = GameObject.Find("WeaponInfo").GetComponent <Text>();
                weaponInfo.color = Color.blue;
                if (equippedWeapon == null)
                {
                    weaponInfo.text = " ";
                }
                else
                {
                    weaponInfo.text = equippedWeapon.getWeaponName() + "\n Durability: " + equippedWeapon.getDurability();
                }
            }
        }
        else if (equippedWeapon == null)
        {
            if (GameObject.Find("WeaponInfo") != null)
            {
                weaponInfo       = GameObject.Find("WeaponInfo").GetComponent <Text>();
                weaponInfo.text  = "No Weapon equipped";
                weaponInfo.color = Color.red;
            }
        }

        //Debug.Log(attacking);
        zombie = zombieDetector.GetNearestZombie();
        if (equippedWeapon != null)
        {
            //if zombie is dead stop attacking
            if (!zombie || zombie.GetComponent <Health>().health <= 0)
            {
                //CancelInvoke("attack");
                attacking = false;
                return;
            }

            Vector3 zombiePosition = zombie.transform.position;
            Vector3 playerPosition = followCamera.GetComponent <FollowCamera>().target.transform.position;
            //distance from zombie to player

            if (Vector3.Distance(zombiePosition, playerPosition) <= equippedWeapon.getAttackingDistance())
            {
                // Debug.Log(Vector3.Distance(zombiePosition, playerPosition));
                // Debug.Log(attacking);
                //only want to call this once so use the attacking flag and check if facing
                if (!attacking)
                {
                    //Debug.DrawLine(playerPosition, (playerPosition + followCamera.GetComponent<FollowCamera>().target.transform.forward * 20), Color.green, 2, false);

                    //Debug.Log("StartAttack");

                    //InvokeRepeating("attack", 0.0f, equippedWeapon.getAttackingDelay());
                    attacking = true;
                }
            }
            else
            {
                //Debug.Log("ZombiePos: " + zombiePosition + "  PlayerPos: " + playerPosition + "   DISTANCE: " + Vector3.Distance(zombiePosition, playerPosition));
                //Debug.Log(zombie.name);
                //if player is not in distance, stop attacking
                //CancelInvoke("attack");
                attacking = false;
            }
        }
        if (attacking)
        {
            PlayerHealth playerHealth = GameObject.Find("PlayerCharacter").GetComponent <PlayerHealth>();
            if (playerHealth.health > 0)
            {
                attack();
            }
        }
    }