// equip a gun public void EquipGun(GunObject gunObj) { DropGun(); AttachGunToPlayer(gunObj); currentGun = gunObj; hasGunEquipped = true; // for this project gunObj.StartCoroutine(gunObj.AutoShotRoutine()); }
public virtual void Update() { // Update gun looking gun.transform.LookAt(leftHand); // Find the nearest control point float bestDistance = Mathf.Infinity; ControlPoint closestPoint = null; foreach (ControlPoint point in FindObjectsOfType <ControlPoint>()) { if (point.ownership != ControlPoint.Ownership.RED && point.influence < 4) { float distance = Vector3.Distance(point.transform.position, transform.position); if (distance < bestDistance) { bestDistance = distance; closestPoint = point; } } } // Find the nearest health pickup float bestHealthDistance = Mathf.Infinity; HealthCrate closestHealth = null; foreach (HealthCrate healthCrate in FindObjectsOfType <HealthCrate>()) { float distance = Vector3.Distance(healthCrate.transform.position, transform.position); if (distance < bestHealthDistance) { bestHealthDistance = distance; closestHealth = healthCrate; } } // Get if there's a player if (player == null) { if (FindObjectOfType <GunManager>() != null) { player = FindObjectOfType <GunManager>().transform; } else { return; } } if (health > 20) { if (CheckIfInSights(player, transform.position)) { state = States.ATTACKING; } else { if (bestDistance > closestPoint.captureDistance) { state = States.ATTACKING_POINT; } else { state = States.DEFENDING_POINT; } } } else { state = States.RETREATING; } // Act on state switch (state) { case States.ATTACKING: agent.isStopped = true; transform.LookAt(player); transform.eulerAngles = new Vector3(0, transform.eulerAngles.y + 8.039f, 0); // Rotate Y-Axis of shooting pos Transform shootingPos = gun.GetComponent <GunObject>().shootingPos; shootingPos.LookAt(player); anim.SetBool("Shooting", true); // Shoot at the player GunObject gunProp = gun.GetComponent <GunObject>(); if (gunProp.CanShoot()) { gunProp.Shoot(); } if (gunProp.ammoInClip <= 0 && !gunProp.isReloading) { gunProp.StartCoroutine(gunProp.Reload()); gunProp.ammoTotal += gunProp.gun.ammoPerMagazine; } break; case States.DEFENDING_POINT: // Defend agent.SetDestination(randomDefencePoint + closestPoint.transform.position); anim.SetBool("Shooting", agent.remainingDistance < 1); break; case States.RETREATING: // Go to nearest health crate agent.isStopped = false; if (closestHealth != null) { agent.SetDestination(closestHealth.transform.position); anim.SetBool("Shooting", false); } else { Debug.Log("No health points found!"); state = States.DEFENDING_POINT; } break; case States.ATTACKING_POINT: if (closestPoint != null) { agent.isStopped = false; if (Vector3.Distance(transform.position, closestPoint.transform.position) >= 15) { agent.SetDestination(closestPoint.transform.position); anim.SetBool("Shooting", false); } else { anim.SetBool("Shooting", false); state = States.DEFENDING_POINT; } } break; } }