private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "FiredObject") { FiredObject fo = other.gameObject.GetComponent <FiredObject>(); health -= fo.damage; hpText.text = "HP: " + health; //Determine if the other object has an explosiveController or not FiredObjectExplosionController foec = other.gameObject.GetComponent <FiredObjectExplosionController>(); if (foec != null) { foec.Explode(); } if (health <= 0) { //Play sound AudioSource.PlayClipAtPoint(hitDeadSound, transform.position); //Award pointValue points to the player playerScoreController.AdjustScore(pointValue); //Remove this obstacle Destroy(this.gameObject); } else { //Else the object should persist, but have a lower health! Play non-death sound AudioSource.PlayClipAtPoint(hitStillHealthSound, transform.position); } } }
/// <summary> /// Cache the needed components and set the beam's color. /// </summary> void Start() { mFo = GetComponent<FiredObject>(); mTrans = transform; mRen = renderer; mMat = mRen.material; mMat.color = color; mIsMine = NetworkManager.IsMine(this); }
/// <summary> /// Cache the needed components and set the beam's color. /// </summary> void Start() { mFo = GetComponent <FiredObject>(); mTrans = transform; mRen = renderer; mMat = mRen.material; mMat.color = color; mIsMine = NetworkManager.IsMine(this); }
/// <summary> /// Fire the weapon. /// </summary> public override void Fire() { if (!mIsPlayerControlled || !canFire) { return; } float remainder = generator.DrainPower(firedObject.energyCost); if (remainder > 0f) { Debug.Log("TODO: Some kind of 'out of power' message"); return; } mNextFire = Time.time + firedObject.firingFrequency; // Instantiate a new object GameObject go = NetworkManager.RemoteInstantiate(prefab, mTrans.position, mTrans.rotation); // The weapon's initial velocity should match the launcher's if (go != null) { if (mCollidersToIgnore != null) { FiredObject fo = go.GetComponent <FiredObject>(); if (fo != null) { fo.ignoreColliders = mCollidersToIgnore; } } NetworkRigidbody nrb = go.GetComponent <NetworkRigidbody>(); if (nrb != null) { if (mRb != null) { nrb.SetVelocity(mRb.velocity + mTrans.rotation * (Vector3.forward * (firedObject.firingVelocity / 3.6f))); } else { nrb.SetVelocity(mTrans.rotation * (Vector3.forward * (firedObject.firingVelocity / 3.6f))); } } else { Debug.LogError("No " + typeof(NetworkRigidbody) + " found on " + Tools.GetHierarchy(go)); } // Any additional functionality OnFire(go); } }
/// <summary> /// Instantiates the provided objectToFire at the firePoint, and uses the transform.forward to apply force to the object /// </summary> /// <param name="point"></param> void InstantiateObjectAndFire() { fireObject = GameObject.Instantiate(objectToFire); fireObject.transform.position = firePoint.position + (firePoint.forward * .2f); //Instantiate it slightly infront of the firePoint //Get the rigidbody on the game object, or add one if not present Rigidbody rigid = fireObject.GetComponent <Rigidbody>(); if (rigid == null) { rigid = fireObject.AddComponent <Rigidbody>(); //If no rigid body on prefab, add a default one to provide some functionality. But allow rigid body effects to be determined by prefab } rigid.AddForce(firePoint.forward * fireForce); //Tag the object with the 'FiredObject' tag (For assistance in collision detection) fireObject.tag = "FiredObject"; AudioSource.PlayClipAtPoint(fireSound, transform.position); //Add the FiredObject script to the object to dictate its behavior (and remove after a certain amount of time) FiredObject fo = fireObject.GetComponent <FiredObject>(); fo.BeginDestructionProcess(timeObjectAlive); }