private void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "hand_deflector") { HandDeflectorBehavior handDeflector = collision.gameObject.GetComponent <HandDeflectorBehavior>(); HandleDeflectorContact(handDeflector); } }
// split the enemy if it gets popped OLD private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "hand_deflector") { HandDeflectorBehavior handDeflector = other.GetComponent <HandDeflectorBehavior>(); Split(handDeflector); } }
// handles the enemy splits private void Split(HandDeflectorBehavior handDeflector) { // prevent from splitting immediately after a previous split if (Time.time < splitTime) { return; } // only creates children if of sufficient size // if the enemy is too small to split, it is destroyed Debug.Log("CURRENT SIZE: " + size); if (size >= splitThreshold) { // calculate how many children to spawn based on current size int numChildren = (int)(splitDensity * size); // adds randomness to spawn position relative to parent enemy float noiseVal = size / 2f; // hard cap the max amount for now if (numChildren > 4) { numChildren = 4; } // for each child for (int i = 0; i < numChildren; i++) { // find spawn position Vector3 posNoise = new Vector3( Random.Range(-noiseVal, noiseVal), Random.Range(-noiseVal, noiseVal), Random.Range(-noiseVal, noiseVal)); Vector3 childPos = transform.position + posNoise; // new size of the child float childSize = size * splitSize; // create a copy of this parent enemy GameObject childEnemy = Instantiate(this.gameObject); // initialize its data EnemyController childController = childEnemy.GetComponent <EnemyController>(); childController.Initialize(target, childPos, childSize); // give it movement due to the deflectors childController.Draft(handDeflector.velocityDeflector); } // play particle effect GameObject particlesSplit = Instantiate(pf_particlesSplit, transform.position, transform.rotation); particlesSplit.transform.Find("Sphere").GetComponent <Rigidbody>().AddForce(handDeflector.velocityDeflector * draftSensitivityParticles); particlesSplit.transform.Find("Small Particles").GetComponent <Rigidbody>().AddForce(handDeflector.velocityDeflector * draftSensitivity); } Destroy(this.gameObject); }
// handles splitting and deflecting when deflector contacts enemy private void HandleDeflectorContact(HandDeflectorBehavior handDeflector) { if (handDeflector.velocityDeflector.magnitude > splitDeflectionSpeedThreshold) { float sizeDifference = size - handDeflector.handSize; // makes sure the user's deflectors are a valid size to pop the enemy if ((size < 0.5f && sizeDifference > 0) || (size >= 0.5f && sizeDifference < 0)) { Split(handDeflector); } } }