//---------------------------------------------- // Simple Car Controller // // Copyright © 2014 - 2023 BoneCracker Games // http://www.bonecrackergames.com // //---------------------------------------------- using UnityEngine; using System.Collections; using System.Collections.Generic; /// /// Handling particle systems on vehicle. Exhausts and wheel particles. /// [AddComponentMenu("BoneCracker Games/Simple Car Controller/SCC Particles")] public class SCC_Particles : MonoBehaviour { // Input processor. private SCC_InputProcessor inputProcessor; public SCC_InputProcessor InputProcessor { get { if (inputProcessor == null) inputProcessor = GetComponent(); return inputProcessor; } } // All wheels of the vehicle. private SCC_Wheel[] wheels; // Exhaust. public ParticleSystem[] exhaustParticles; private ParticleSystem.EmissionModule[] exhaustEmissions; // Wheel particle prefab will be instantiated for each wheel. public ParticleSystem wheelParticlePrefab; // Created wheel particles for all wheels. private List createdParticles = new List(); private ParticleSystem.EmissionModule[] wheelEmissions; public float slip = .25f; // If wheel sideways or forward skid exceeds this limit slip, emission will be enabled. private void Awake() { // Getting all wheels of the vehicle. wheels = GetComponentsInChildren(); // If wheel particle is selected, instantiate it per each wheel. Disable their emissions at start, and store them in the list. if (wheelParticlePrefab) { for (int i = 0; i < wheels.Length; i++) { ParticleSystem particle = Instantiate(wheelParticlePrefab, wheels[i].transform.position, wheels[i].transform.rotation, wheels[i].transform); createdParticles.Add(particle.GetComponent()); } wheelEmissions = new ParticleSystem.EmissionModule[createdParticles.Count]; for (int i = 0; i < createdParticles.Count; i++) wheelEmissions[i] = createdParticles[i].emission; } // If exhaust particles selected, disable their emissions at start. if (exhaustParticles != null && exhaustParticles.Length >= 1) { exhaustEmissions = new ParticleSystem.EmissionModule[exhaustParticles.Length]; for (int i = 0; i < exhaustParticles.Length; i++) exhaustEmissions[i] = exhaustParticles[i].emission; } } private void Update() { WheelParticles(); ExhaustParticles(); } /// /// Enabling / disabling emission of the wheel particles. /// private void WheelParticles() { // If wheel particle is not selected, return. if (!wheelParticlePrefab) return; // If wheel particle is not selected, return. if (createdParticles.Count < 1) return; // Getting sideways and forward slips of the each wheel and deciding to enable emission or not. for (int i = 0; i < wheels.Length; i++) { WheelHit hit; wheels[i].WheelCollider.GetGroundHit(out hit); if (Mathf.Abs(hit.sidewaysSlip) >= slip || Mathf.Abs(hit.forwardSlip) >= slip) wheelEmissions[i].enabled = true; else wheelEmissions[i].enabled = false; } } /// /// Enabling / disabling emission of the exhaust particles. /// private void ExhaustParticles() { if (exhaustParticles.Length < 1) return; for (int i = 0; i < exhaustEmissions.Length; i++) exhaustEmissions[i].rate = Mathf.Lerp(1f, 20f, InputProcessor.inputs.throttleInput); } }