Nextrek
@ 168
Nextrek / 3DSpace / Assets / SpaceUnity / _Demo / Scrips / SU_Thruster.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
// Thruster C# Script (version: 1.02) // SPACE UNITY - Space Scene Construction Kit // http://www.spaceunity.com // (c) 2013 Stefan Persson // DESCRIPTION: // This script controls the thruster used for spaceship propulsion. // INSTRUCTIONS: // Use the thruster prefab which has the required particle system this script depends upon. // Configure the thruster force and other parameters as desired. // PARAMETERS: // thrusterForce (the thruster force to be applied to it's rigidbody parent when active // addForceAtPosition (whether or not to introduce torque/rotation... use with care as it is super sensitive for positioning) // soundEffectVolume (sound effect volume of thruster) // Version History // 1.02 - Prefixed with SU_Thruster to avoid naming conflicts. // 1.01 - Initial Release. using UnityEngine; using System.Collections; public class SU_Thruster : MonoBehaviour { // Force of individual thrusters public float thrusterForce = 5000; public float burstForce = 40000; // Whether or not to add force at position which introduces torque, use with care... public bool addForceAtPosition = false; // Sound effect volume of thruster public float soundEffectVolume = 1.0f; // Private variables private bool _isActive = false; private bool _isBurst = false; private float _burstTotalTime = 5; private float _burstTime = 0; private Transform _cacheTransform; private Rigidbody _cacheParentRigidbody; private Light _cacheLight; private ParticleSystem _cacheParticleSystem; // Call StartThruster() function from other scripts to start the thruster public void SwitchThruster() { _isActive = !_isActive; } public void StartThruster() { // Set the thruster active flag to true _isActive = true; } // Call StopThruster() function from other scripts to stop the thruster public void StopThruster() { // Set the thruster active flag to false _isActive = false; } public void StartBurst() { _burstTime = 0; _isBurst = true; } void Start () { // Cache the transform and parent rigidbody to improve performance _cacheTransform = transform; // Ensure that parent object (e.g. spaceship) has a rigidbody component so it can apply force. if (transform.parent.GetComponent<Rigidbody>() != null) { _cacheParentRigidbody = transform.parent.GetComponent<Rigidbody>(); } else { Debug.LogError("Thruster has no parent with rigidbody that it can apply the force to."); } // Cache the light source to improve performance (also ensure that the light source in the prefab is intact.) _cacheLight = transform.GetComponent<Light>().GetComponent<Light>(); if (_cacheLight == null) { Debug.LogError("Thruster prefab has lost its child light. Recreate the thruster using the original prefab."); } // Cache the particle system to improve performance (also ensure that the particle system in the rpefab is intact.) _cacheParticleSystem = GetComponent<ParticleSystem>(); if (_cacheParticleSystem == null) { Debug.LogError("Thruster has no particle system. Recreate the thruster using the original prefab."); } // Start the audio loop playing but mute it. This is to avoid play/stop clicks and clitches that Unity may produce. GetComponent<AudioSource>().loop = true; GetComponent<AudioSource>().volume = soundEffectVolume; GetComponent<AudioSource>().mute = true; GetComponent<AudioSource>().Play(); } void Update () { if(_isBurst) { _burstTime+=Time.deltaTime; if(_burstTime>_burstTotalTime) { _burstTime = 0; _isBurst = false; } } // If the light source of the thruster is intact... if (_cacheLight != null) { // Set the intensity based on the number of particles _cacheLight.intensity = _cacheParticleSystem.particleCount / 20; } // If the thruster is active... if (_isActive) { // ...and if audio is muted... if (GetComponent<AudioSource>().mute) { // Unmute the audio GetComponent<AudioSource>().mute=false; } // If the audio volume is lower than the sound effect volume... if (GetComponent<AudioSource>().volume < soundEffectVolume) { // ...fade in the sound (to avoid clicks if just played straight away) GetComponent<AudioSource>().volume += 5f * Time.deltaTime; } // If the particle system is intact... if (_cacheParticleSystem != null) { // Enable emission of thruster particles _cacheParticleSystem.enableEmission = true; } } else { // The thruster is not active if (GetComponent<AudioSource>().volume > 0.01f) { // ...fade out volume GetComponent<AudioSource>().volume -= 5f * Time.deltaTime; } else { // ...and mute it when it has faded out GetComponent<AudioSource>().mute = true; } // If the particle system is intact... if (_cacheParticleSystem != null) { // Stop emission of thruster particles _cacheParticleSystem.enableEmission = false; } } } void FixedUpdate() { // If the thruster is active... if (_isActive) { // ...add the relative thruster force to the parent object float force = thrusterForce; if(_isBurst) { force = burstForce; } if (addForceAtPosition) { // Add force relative to the position on the parent object which will also apply rotational torque _cacheParentRigidbody.AddForceAtPosition (_cacheTransform.up * force, _cacheTransform.position); } else { // Add force without rotational torque _cacheParentRigidbody.AddRelativeForce (Vector3.forward * force); } } } } |