void Awake() { // Setup references player = GameObject.FindGameObjectWithTag("Player").transform; playerHealth = player.GetComponent <SandCharHealth>(); enemyHealth = GetComponent <SandEnemyHealth>(); nav = GetComponent <NavMeshAgent>(); }
void Shoot() { // Reset timer timer = 0f; // Play gun audio //gunAudio.Play(); // Flash gun's light //gunLight.enabled = true; // Start/reset gun particle effects //gunParticles.Stop(); //gunParticles.Play(); // Set first point of visible shot line at gun barrel shotLine.enabled = true; shotLine.SetPosition(0, transform.position); // Set first point & direction of shotFired ray shotFired.origin = transform.position; shotFired.direction = transform.forward; // Raycast against shootable objects; if it hits anything... if (Physics.Raycast(shotFired, out shotHit, shotRange, shootableMask)) { // See if hit object has EnemyHealth script SandEnemyHealth enemyHealth = shotHit.collider.GetComponent <SandEnemyHealth>(); // If hit object does have EnemyHelth, it takes damage if (enemyHealth != null) { enemyHealth.TakeDamage(damageAmount, shotHit.point); } // Set end of line renderer at hit object shotLine.SetPosition(1, shotHit.point); } // Otherwise, if didn't hit anything shootable else { // Extend gunshot line out to range limit shotLine.SetPosition(1, shotFired.origin + shotFired.direction * shotRange); } }
private void OnTriggerEnter(Collider other) { // If colliding with an enemy hitbox... if (other.CompareTag("Enemy")) { // ... set hitable to true hitable = true; // Also make note of enemy being hit (so we know who's health to decrease) enemyHealth = other.GetComponentInParent <SandEnemyHealth>(); } // If player collides with head collider on enemy... if (other.GetType() == typeof(SphereCollider)) { // ... destroy this enemy Destroy(other.gameObject); } }
void Awake() { // Setup references enemyHealth = GetComponent <SandEnemyHealth>(); nav = GetComponent <NavMeshAgent>(); // Setup patrol bounds based on x or z axis if (patrolXSize == 0) { patrolZBoundMin = transform.position.z - patrolZSize; patrolZBoundMax = transform.position.z + patrolZSize; // Start by moving left towards patrolBoundMin nav.SetDestination(new Vector3(transform.position.x, transform.position.y, patrolZBoundMin)); } else if (patrolZSize == 0) { patrolXBoundMin = transform.position.x - patrolXSize; patrolXBoundMax = transform.position.x + patrolXSize; // Start by moving left towards patrolBoundMin nav.SetDestination(new Vector3(patrolXBoundMin, transform.position.y, transform.position.z)); } }