private void FireGun() { // Play muzzle flash particle effect if (muzzleFlash != null) { muzzleFlash.Emit(1); } // Turn on muzzle flash point light if (muzzleLight != null && !muzzleLight.enabled) { muzzleLight.enabled = true; Invoke("DisableMuzzleLight", 0.1f); } WeaponHelper.Launch(bulletPrefab, gunTransform, 0f, false); // Launch bullet lastShotTime = Time.time; }
private void Update() { if (nukeTimer > 0f) { nukeTimer -= Time.deltaTime; } if (nukeTimer < 0f) { nukeTimer = 0f; } if (rocketTimer > 0f) { rocketTimer -= Time.deltaTime; } if (rocketTimer <= 0f) { if (launcherRocket != null) { launcherRocket.SetActive(true); } rocketTimer = 0f; } if (Input.GetButtonDown("Fire1")) { //Cursor.lockState = CursorLockMode.Locked; switch (SelectedWeapon) { case WeaponType.Cannonball: if (cannonFire != null) { cannonFire.GetComponent <ParticleSystem>().Clear(true); cannonFire.Play(true); } WeaponHelper.Launch(cannonballPrefab, cannonTransform, startDistance, cannonballVelocity, true); break; case WeaponType.Rocket: if (rocketTimer <= 0f) { if (launcherRocket != null) { launcherRocket.SetActive(false); } if (rocketFire != null) { rocketFire.GetComponent <ParticleSystem>().Clear(true); rocketFire.Play(true); } WeaponHelper.Launch(rocketPrefab, rocketTransform, startDistance + .1f, 6f, false); RocketLoading rocketLoading = launcherRocket.GetComponentInChildren <RocketLoading>(); if (rocketLoading != null) { rocketLoading.isLoaded = false; } rocketTimer = 1f; // one rocket every X seconds. } break; case WeaponType.Nuke: // Nuclear Blast and Rolling Shockwave Damage if (nukeTimer <= 0f) { FadeIn flashEffect = gameObject.AddComponent <FadeIn>(); flashEffect.startColor = Color.white; flashEffect.fadeLength = 5f; // position the nuke 2500m in front of where the player is facing. Transform player = GameObject.FindGameObjectWithTag("Player").transform; Vector3 nukeForwardPos = player.position + player.forward * nukeDistance; Vector3 nukePos = new Vector3(nukeForwardPos.x, 0f, nukeForwardPos.z); if (groundChurnPrefab != null) { GameObject groundChurn = Instantiate(groundChurnPrefab, nukePos, Quaternion.identity) as GameObject; Follow followScript = groundChurn.AddComponent <Follow>(); followScript.isPositionFixed = true; followScript.objectToFollow = player; followScript.facingDirection = FacingDirection.FixedPosition; followScript.fixedFromPosition = nukePos; followScript.fixedDistance = groundChurnDistance; } GameObject nuke = Instantiate(nukePrefab, nukePos, Quaternion.Euler(Vector3.zero)) as GameObject; nuke.transform.LookAt(player); // Configure Wind Zone if (windZone != null) { windZone.transform.position = nukeForwardPos; windZone.transform.LookAt(player); Invoke("EnableWindZone", 5f); DisableAfter disableAfter = windZone.GetComponent <DisableAfter>() ?? windZone.AddComponent <DisableAfter>(); disableAfter.seconds = 25f; disableAfter.removeScript = true; } // Configure Dust Wall if (dustWallPrefab != null) { GameObject dustWall = Instantiate(dustWallPrefab, nukeForwardPos, Quaternion.Euler(Vector3.zero)) as GameObject; dustWall.transform.LookAt(player); dustWall.transform.position += (dustWall.transform.forward * dustWallDistance); dustWall.GetComponent <Rigidbody>().AddForce(dustWall.transform.forward * shockwaveSpeed, ForceMode.Force); DustWall dwScript = dustWall.GetComponent <DustWall>(); dwScript.fixedFromPosition = nukePos; } // Configure Shock Wall if (shockWallPrefab != null) { GameObject shockWall = Instantiate(shockWallPrefab, nukeForwardPos, Quaternion.Euler(Vector3.zero)) as GameObject; shockWall.transform.LookAt(player); shockWall.GetComponent <Rigidbody>().AddForce(shockWall.transform.forward * shockwaveSpeed, ForceMode.Force); ShockWall swScript = shockWall.GetComponent <ShockWall>(); swScript.origin = nukePos; } Invoke("BroadcastNukeStart", 0.1f); Invoke("BroadcastNukeEnd", 25f); nukeTimer = 30f; // only one nuke every x seconds. } break; case WeaponType.Gun: FireGun(); break; case WeaponType.Melee: if (Time.time >= (lastMeleeTime + meleeAttackDelay)) { MeleeAttack(); } break; case WeaponType.RepairWrench: if (Time.time >= (lastMeleeTime + meleeAttackDelay)) { RepairByHand(); } break; } } // Continuous fire from holding the button down if (Input.GetButton("Fire1") && SelectedWeapon == WeaponType.Gun && Time.time >= (lastShotTime + timeBetweenShots)) { FireGun(); } // Continuous melee attack from holding the button down (useful for chopping trees in an MMO/survival game) if (Input.GetButton("Fire1") && SelectedWeapon == WeaponType.Melee && Time.time >= (lastMeleeTime + meleeAttackDelay)) { MeleeAttack(); } // Repair continuously while holding the Fire1 button down. if (Input.GetButton("Fire1") && SelectedWeapon == WeaponType.RepairWrench && Time.time >= (lastMeleeTime + meleeAttackDelay)) { RepairByHand(); } // Time Slow if (Input.GetKeyUp("t")) { timeSlowed = !timeSlowed; SetTimeScale(); } // Time Stop if (Input.GetKeyUp("y")) { timeStopped = !timeStopped; SetTimeScale(); } /* DISABLING RIGIDBODY INTERPOLATION TEMPORARILY DUE TO AN ONGOING UNITY BUG: * https://issuetracker.unity3d.com/issues/in-order-to-call-gettransforminfoexpectuptodate-dot-dot-dot-error-message-appears-while-using-rigidbody-interpolate-slash-extrapolate * * // Do this every frame for rigidbodies that enter the scene, so they have smooth frame interpolation. * // TODO: can probably run this more efficiently at a set rate, like a few times per second - not every frame. * if (timeSlowed) * { * foreach (GameObject go in FindObjectsOfType(typeof(GameObject))) * { * foreach (Rigidbody rb in go.GetComponentsInChildren<Rigidbody>()) * rb.interpolation = RigidbodyInterpolation.Interpolate; * } * } */ // Reset the scene if (Input.GetKey("r")) { TreeManager treeManager = TreeManager.Instance; if (treeManager != null) { //TODO: Black out the main camera first. You can see trees insta-respawn when restored, and it looks weird. treeManager.RestoreTrees(); } SceneManager.LoadScene(SceneManager.GetActiveScene().name); } if (Input.GetKeyUp("q")) { SelectedWeapon = WeaponHelper.GetPrevious(SelectedWeapon); PlayerPrefs.SetInt("SelectedWeapon", (int)SelectedWeapon); SetActiveWeapon(); } if (Input.GetKeyUp("e")) { SelectedWeapon = WeaponHelper.GetNext(SelectedWeapon); PlayerPrefs.SetInt("SelectedWeapon", (int)SelectedWeapon); SetActiveWeapon(); } if (Input.GetKeyUp("o")) { if (playerPrefShowReticle == -1) { playerPrefShowReticle = 0; } else { playerPrefShowReticle = -1; } PlayerPrefs.SetInt("ShowReticle", playerPrefShowReticle); } if (Input.GetKeyUp("h")) { if (playerPrefShowHud == -1) { playerPrefShowHud = 0; } else { playerPrefShowHud = -1; } PlayerPrefs.SetInt("ShowHud", playerPrefShowHud); } if (Input.GetKeyUp("m")) { Cursor.lockState = CursorLockMode.None; Cursor.visible = true; // Always restore trees before changing scenes so you don't lose terrain tree data in the Unity editor. TreeManager treeManager = TreeManager.Instance; if (treeManager != null) { treeManager.RestoreTrees(); } SceneManager.LoadScene("Choose Demo"); } float scrollWheel = Input.GetAxis("Mouse ScrollWheel"); if (scrollWheel > 0f) // scroll up { SelectedWeapon = WeaponHelper.GetNext(SelectedWeapon); PlayerPrefs.SetInt("SelectedWeapon", (int)SelectedWeapon); SetActiveWeapon(); } if (scrollWheel < 0f) // scroll down { SelectedWeapon = WeaponHelper.GetPrevious(SelectedWeapon); PlayerPrefs.SetInt("SelectedWeapon", (int)SelectedWeapon); SetActiveWeapon(); } }