// 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(); } } }
public void equipWeapon() { GameObject equippedWeaponObject = GameObject.Find("EquippedWeapon"); EquippedWeapon equipped = equippedWeaponObject.GetComponent <EquippedWeapon>(); if (equipped.equippedWeapon != null && (equipped.equippedWeapon.getWeaponName() == weapon.getWeaponName())) { freezeRigidbody(); return; } else { if (equipped.equippedWeapon != null) { dropWeapon(); } } //remove components Destroy(GetComponent <Rigidbody>()); Destroy(GetComponent <MeshCollider>()); //set weapon position to parents Transform equippedWeaponTransform = equippedWeaponObject.GetComponent <Transform>(); Transform groundWeaponTransform = GetComponent <Transform>(); groundWeaponTransform.SetPositionAndRotation(equippedWeaponTransform.position, equippedWeaponTransform.rotation); groundWeaponTransform.parent = equippedWeaponTransform; //assign the equipped weapon equipped.equippedWeapon = weapon; equipped.playPickUpSound(); }