Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class ClusterMissileProjectile : MonoBehaviour {
- public float ExplodeDistance = 3;
- public float MissileSpeed = 5;
- public float ClusterAmount = 3;
- public float ClusterSpreadDegrees = 120;
- public int ChildrenToSpawn = 3;
- private Vector3 _startPosition;
- // Use this for initialization
- void Start () {
- _startPosition = transform.position;
- }
- // Update is called once per frame
- void Update () {
- if(Vector3.Distance(_startPosition, transform.position) >= ExplodeDistance){
- Explode();
- } else {
- transform.Translate(Vector3.up * MissileSpeed * Time.deltaTime);
- }
- }
- /// <summary>
- /// Explode this instance.
- /// </summary>
- void Explode(){
- //this value will go down each time a new set of children is spawned
- if(ChildrenToSpawn > 0){
- var spreadMax = ClusterSpreadDegrees / 2;
- var spreadDistance = ClusterSpreadDegrees / (ClusterAmount-1);
- //initial rotation (in this case -22.5)
- var rotationAmountDegrees = -spreadMax;
- for (int i = 0; i < ClusterAmount; i++) {
- var newMissile = Instantiate(this.gameObject, transform.position, transform.rotation) as GameObject;
- //Vector3.right is towards the camera
- newMissile.transform.RotateAround(transform.position, Vector3.right, rotationAmountDegrees);
- rotationAmountDegrees += spreadDistance;
- //make sure we don't create these infinitely
- newMissile.GetComponent<ClusterMissileProjectile>().ChildrenToSpawn -= 1;
- }
- }
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement