// Update is called once per frame void Update() { // Acquire the nearest target Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, attackRadius); float minDistance = float.MaxValue; IUnit nearestEnemy = null; for (int i = 0; i < nearbyObjects.Length; i++) { if (nearbyObjects[i].transform.childCount > 0) { IUnit target = (IUnit)nearbyObjects[i].gameObject.GetComponent(typeof(IUnit)); if (target != null && !target.isUnitDying() && !target.isUnitAlly()) { float distance = Vector3.Distance(transform.position, target.getGameObject().transform.position); if (distance < minDistance) { minDistance = distance; nearestEnemy = target; } } } } if (nearestEnemy != null) { // Turn to face the enemy transform.rotation = Quaternion.LookRotation(nearestEnemy.getGameObject().transform.position - transform.position); transform.rotation = Quaternion.Euler(0, transform.eulerAngles.y, 0); // Launch bolt as long as an enemy is found boltTimer++; if (boltTimer > emissionFrequency && !firing) { boltTimer = 0; Vector3 velocity = transform.forward.normalized; Vector3 startPosition = transform.position + velocity * 1; // Apply the correct velocity to the emission velocity *= emissionVelocity; GameObject newbolt = createbolt(startPosition, transform.rotation, velocity, 0); MoveBolt bolt = (MoveBolt)newbolt.GetComponent(typeof(MoveBolt)); bolt.setTarget(nearestEnemy.getGameObject()); // Create Coroutine to stop firing after playing the animation once StartCoroutine(shootBolt()); } } else { transform.rotation = Quaternion.identity; } }
public GameObject createbolt(Vector3 position, Quaternion rotation, Vector3 velocity, int hashValue) { GameObject newbolt = (GameObject)Instantiate(bolt, position - boltHeightOffset, rotation); newbolt.transform.parent = GetObjects.instance.getAttackParticleContainer(); newbolt.SetActive(true); MoveBolt moveThis = (MoveBolt)newbolt.GetComponent(typeof(MoveBolt)); moveThis.enabled = true; moveThis.setVelocity(velocity); moveThis.setHash(0); return(newbolt); }
void OnCollisionEnter(Collision collision) { if (collision.gameObject == null) { return; } GameObject other = collision.gameObject; IUnit enemy = (IUnit)other.GetComponent(typeof(IUnit)); MoveBolt moveScript = (MoveBolt)other.GetComponent((typeof(MoveBolt))); // Ignore collisions with other bolts if (moveScript != null) { //print ("Colliding with another bolt"); } // Ignore collisions with player's hands else if (other.name == "palm" || other.name == "bone1" || other.name == "bone2") { } // Check if a player or their leap motion hands are hit else if (other.name == "LeapOVRPlayerController") { } // Collide with an enemy else if (enemy != null) { enemy.dealDamage(damage, Vector3.zero); Destroy(gameObject); } else { Destroy(gameObject); } }