IEnumerator ChainReact(int chainDepth) { Collider[] allNear = Physics.OverlapSphere(transform.position, blastRadius); for (int i = 0; i < allNear.Length; i++) { ExplodeChainReact ecrScript = allNear[i].gameObject.GetComponent <ExplodeChainReact>(); if (ecrScript && gameObject != allNear[i].gameObject) // can explode, and isn't self { Debug.Log("CHAIN (" + chainDepth + ") from " + gameObject.name + " to " + allNear[i].gameObject.name); ecrScript.Explode(chainDepth + 1); } } yield return(new WaitForSeconds(chainDepth * Random.Range(0.1f, 0.4f))); // lag the explosion GameObject pointGO = GameObject.Instantiate(pointPopper, transform.position, transform.rotation); PointScaleFadeDie psfdScript = pointGO.GetComponent <PointScaleFadeDie>(); psfdScript.SetText(chainDepth + "00", chainDepth); ScoreKeeper.instance.AddScore(100 * chainDepth); GameObject.Instantiate(blastVFX, transform.position, transform.rotation); Destroy(gameObject); }
public int HitsInRange() { if (alreadyCountedInChainForAimThisFrame) { return(0); // already visited - also avoids recusive/feedback lock up } int hitsThroughMe = 1; // counts itself alreadyCountedInChainForAimThisFrame = true; // will stop from revisiting itself Collider[] allNear = Physics.OverlapSphere(transform.position, blastRadius); for (int i = 0; i < allNear.Length; i++) { ExplodeChainReact ecrScriptNext = allNear[i].gameObject.GetComponent <ExplodeChainReact>(); if (ecrScriptNext && ecrScriptNext.alreadyCountedInChainForAimThisFrame == false) { hitsThroughMe += ecrScriptNext.HitsInRange(); } } return(hitsThroughMe); }
void Update() { float thicknessRange = 0.4f; RaycastHit[] laserMiddle = Physics.RaycastAll(transform.position, transform.forward); RaycastHit[] laserLeft = Physics.RaycastAll(transform.position - transform.right * thicknessRange, transform.forward); RaycastHit[] laserRight = Physics.RaycastAll(transform.position + transform.right * thicknessRange, transform.forward); RaycastHit[] laserUp = Physics.RaycastAll(transform.position + transform.up * thicknessRange, transform.forward); RaycastHit[] laserDown = Physics.RaycastAll(transform.position - transform.up * thicknessRange, transform.forward); float roundedPerc = 0.707f; RaycastHit[] laserUL = Physics.RaycastAll(transform.position + transform.up * thicknessRange * roundedPerc - transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserUR = Physics.RaycastAll(transform.position + transform.up * thicknessRange * roundedPerc + transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserDL = Physics.RaycastAll(transform.position - transform.up * thicknessRange * roundedPerc - transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserDR = Physics.RaycastAll(transform.position - transform.up * thicknessRange * roundedPerc + transform.right * thicknessRange * roundedPerc, transform.forward); List <RaycastHit> rhListTemp = new List <RaycastHit>(); AddArrayIntoListIfUnique(rhListTemp, laserLeft); AddArrayIntoListIfUnique(rhListTemp, laserRight); AddArrayIntoListIfUnique(rhListTemp, laserUp); AddArrayIntoListIfUnique(rhListTemp, laserDown); AddArrayIntoListIfUnique(rhListTemp, laserUL); AddArrayIntoListIfUnique(rhListTemp, laserUR); AddArrayIntoListIfUnique(rhListTemp, laserDL); AddArrayIntoListIfUnique(rhListTemp, laserDR); AddArrayIntoListIfUnique(rhListTemp, laserMiddle); RaycastHit[] allRH = rhListTemp.ToArray(); // Physics.RaycastAll(transform.position, transform.forward); int hitsInRange = 0; for (int i = 0; i < allRH.Length; i++) { RaycastHit rhInfo = allRH[i]; // crosshair.rectTransform.position = Camera.main.WorldToScreenPoint(rhInfo.point); lastAimedRange = Vector3.Distance(transform.position, rhInfo.point); crosshair.color = Color.red; ExplodeChainReact ecrScript = rhInfo.collider.gameObject.GetComponent <ExplodeChainReact>(); if (Input.GetMouseButtonDown(0)) { emitter.Emit(1000); Debug.Log("DIRECT HIT:" + rhInfo.collider.gameObject.name); if (ecrScript) { ecrScript.Explode(1); } } else { if (ecrScript) { hitsInRange += ecrScript.HitsInRange(); } else { cannonReadout.text = "X"; } } } if (lastAimedRange > maxAimRange) { lastAimedRange = maxAimRange; } crosshair.rectTransform.position = Camera.main.WorldToScreenPoint(transform.position + transform.forward * lastAimedRange); if (allRH.Length == 0) // essentially, else / nothing under gun { crosshair.color = Color.cyan; cannonReadout.text = "0"; } else if (hitsInRange != 0) { cannonReadout.text = "" + hitsInRange; } else { cannonReadout.text = "X"; } }
void Update() { bool fireTriggerSqueezingNow = Input.GetAxis("TriggerAxis") > 0.2f; bool fireTriggerSqueezedFrame = (fireTriggerSqueezingNow && fireHoldLock == false); fireHoldLock = fireTriggerSqueezingNow; if (Input.GetKeyDown(KeyCode.Escape)) { SceneManager.LoadScene(0); // back to menu } if (lastAimedRange < 25.0f) { lastAimedRange = 25.0f; } crosshair.rectTransform.position = Camera.main.WorldToScreenPoint(transform.position + transform.forward * lastAimedRange); if (shotsLeft <= 0) { crosshair.color = Color.black; cannonReadout.text = "-"; outOfAmmo?.Invoke(); return; } float thicknessRange = 0.4f; RaycastHit[] laserMiddle = Physics.RaycastAll(transform.position, transform.forward); RaycastHit[] laserLeft = Physics.RaycastAll(transform.position - transform.right * thicknessRange, transform.forward); RaycastHit[] laserRight = Physics.RaycastAll(transform.position + transform.right * thicknessRange, transform.forward); RaycastHit[] laserUp = Physics.RaycastAll(transform.position + transform.up * thicknessRange, transform.forward); RaycastHit[] laserDown = Physics.RaycastAll(transform.position - transform.up * thicknessRange, transform.forward); float roundedPerc = 0.707f; RaycastHit[] laserUL = Physics.RaycastAll(transform.position + transform.up * thicknessRange * roundedPerc - transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserUR = Physics.RaycastAll(transform.position + transform.up * thicknessRange * roundedPerc + transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserDL = Physics.RaycastAll(transform.position - transform.up * thicknessRange * roundedPerc - transform.right * thicknessRange * roundedPerc, transform.forward); RaycastHit[] laserDR = Physics.RaycastAll(transform.position - transform.up * thicknessRange * roundedPerc + transform.right * thicknessRange * roundedPerc, transform.forward); List <RaycastHit> rhListTemp = new List <RaycastHit>(); AddArrayIntoListIfUnique(rhListTemp, laserLeft); AddArrayIntoListIfUnique(rhListTemp, laserRight); AddArrayIntoListIfUnique(rhListTemp, laserUp); AddArrayIntoListIfUnique(rhListTemp, laserDown); AddArrayIntoListIfUnique(rhListTemp, laserUL); AddArrayIntoListIfUnique(rhListTemp, laserUR); AddArrayIntoListIfUnique(rhListTemp, laserDL); AddArrayIntoListIfUnique(rhListTemp, laserDR); AddArrayIntoListIfUnique(rhListTemp, laserMiddle); RaycastHit[] allRH = rhListTemp.ToArray(); // Physics.RaycastAll(transform.position, transform.forward); int hitsInRange = 0; bool didFire = false; crosshair.color = Color.red; for (int i = 0; i < allRH.Length; i++) { RaycastHit rhInfo = allRH[i]; crosshair.rectTransform.position = Camera.main.WorldToScreenPoint(rhInfo.point); lastAimedRange = Vector3.Distance(transform.position, rhInfo.point); ExplodeChainReact ecrScript = rhInfo.collider.gameObject.GetComponentInParent <ExplodeChainReact>(); bool isItShootingPoint = (ShootingPoints ? rhInfo.collider.gameObject.transform.IsChildOf(ShootingPoints.transform) : false); if (Input.GetMouseButtonDown(0) || fireTriggerSqueezedFrame) { if (isItShootingPoint) { transform.root.position = rhInfo.collider.transform.position; transform.root.rotation = rhInfo.collider.transform.rotation; break; // shooting point, bail out } else { didFire = true; // setting only once, to not repeat per sub raycast /*GameObject testBlastVFX = Resources.Load("Explosion5m") as GameObject; * GameObject pointGO = GameObject.Instantiate(testBlastVFX, transform.position + transform.forward * 7.0f, * transform.rotation);*/ Invoke(nameof(DisableMuzzleFlash), muzzleFlashDuration); // Debug.Log("DIRECT HIT:" + rhInfo.collider.gameObject.name); if (ecrScript) { ecrScript.Explode(1); } } } else { if (ecrScript) { hitsInRange += ecrScript.HitsInRange(); } else if (isItShootingPoint) { crosshair.color = Color.green; } else { cannonReadout.text = "X"; } } } if (didFire) { shotsLeft--; UpdateAmmoDisplay(); emitter.Emit(1000); animator.SetTrigger("Fire"); cannonShotSound.clip = cannonSoundClips[Random.Range(0, cannonSoundClips.Count)]; cannonShotSound.Play(); muzzleFlash.SetActive(true); if (SceneManager.GetActiveScene().name == "ChaosDimension") { Debug.Log("shot fired in Chaos Dimension"); TimeKeeper.instance.RandomJoltTimeOffset(); } TimeKeeper.instance.fakeTimePace = 1.0f; } if (lastAimedRange > maxAimRange) { lastAimedRange = maxAimRange; } if (allRH.Length == 0) // essentially, else / nothing under gun { crosshair.color = Color.red; cannonReadout.text = "0"; } else if (hitsInRange != 0) { crosshair.color = Color.cyan; cannonReadout.text = "" + hitsInRange; } else { crosshair.color = Color.black; cannonReadout.text = "X"; } }
IEnumerator ChainReact(int chainDepth) { Collider[] allNear = Physics.OverlapSphere(transform.position, blastRadius); for (int i = 0; i < allNear.Length; i++) { ExplodeChainReact ecrScript = allNear[i].gameObject.GetComponentInParent <ExplodeChainReact>(); if (ecrScript && gameObject != allNear[i].gameObject) // can explode, and isn't self { Debug.Log("CHAIN (" + chainDepth + ") from " + gameObject.name + " to " + allNear[i].gameObject.name); ecrScript.Explode(chainDepth + 1); } } yield return(new WaitForSeconds(chainDepth * Random.Range(0.1f, 0.4f))); // lag the explosion Destroy(gameObject); GameObject pointGO = GameObject.Instantiate(pointPopper, transform.position, transform.rotation); PointScaleFadeDie psfdScript = pointGO.GetComponent <PointScaleFadeDie>(); int score = chainDepth * 100; psfdScript.SetText(score, chainDepth); ScoreKeeper.instance.AddScore(score); // UI Elements Handling if (TotalNumberOfTargetHits && ScoreList) { TotalNumberOfTargetHits.value += 1; if (chainDepth == 1) { ScoreList.value.Add(score); NumberOfTargetsList.value.Add(chainDepth); } else if (chainDepth > 1) { int index; index = ScoreList.value.Count - 1; if (index >= 0 && index < ScoreList.value.Count) { ScoreList.value[ScoreList.value.Count - 1] += score; } else { Debug.LogWarning("INVALID INDEX attempt blocked"); } index = NumberOfTargetsList.value.Count - 1; if (index >= 0 && index < NumberOfTargetsList.value.Count) { NumberOfTargetsList.value[NumberOfTargetsList.value.Count - 1] += 1; } else { Debug.LogWarning("INVALID INDEX attempt blocked"); } } } else { Debug.LogWarning("NOTE: TotalNumberOfTargetHits AND/OR ScoreList isn't set in this scene"); } }