void OnDestroy() { if (instance_ == this) { instance_ = null; } }
void Awake() { if (instance_ == null) { instance_ = this; } if (pool_.Count == 0 && active_pool_.Count == 0) { for (int i = 0; i < 30; ++i) { GameObject instance = GameObject.Instantiate(instance_.prefab_); instance.SetActive(false); pool_.Add(instance); } } }
public void FireAtTarget(Vector3 at) { GameObject cannon_ball_obj = CannonballPool.Create(transform) as GameObject; CannonballBehaviour cannon_ball_cmp = cannon_ball_obj.GetComponent <CannonballBehaviour>(); float angle_rad = cannon_ball_cmp.Fire(at); float angle_deg = angle_rad * Mathf.Rad2Deg; transform.localRotation = Quaternion.AngleAxis(-(angle_deg + 90.0f), Vector3.right); Transform parent_transform = transform.parent; Vector3 dir = cannon_ball_cmp.GetFlatDirection(parent_transform.position, at); parent_transform.rotation = Quaternion.LookRotation(dir); }
void Update() { if (trajectory_ != null) { // Move the cannon ball. Vector3 new_pos = transform.position; new_pos += transform.forward * speed_ * Time.deltaTime * speed_multiplier_; float distance_to_start = GetFlatDistance(new_pos, start_pos_); if (distance_to_start > start_distance_) { // if we passed our target, make sure we land at the end. transform.position = target_; trajectory_ = null; OnImpact(target_); } else if (called_near_impact_ == false && ((distance_to_start / start_distance_) > 0.7f)) { // if we passed our target, make sure we land at the end. OnNearImpact(target_); } else { // Otherwise, proceed by getting new height. new_pos.y = trajectory_.GetHeightAtDistance(distance_to_start); transform.position = new_pos; } } if (timer_ > 0.0f) { timer_ -= Time.deltaTime; if (timer_ <= 0.0f) { CannonballPool.Destroy(gameObject); if (explosion_ != null) { explosion_.SetActive(false); } } } }