using UnityEngine; public class SkidMarks : MonoBehaviour { [SerializeField] private GameObject _driftEffect; private VehicleController _attachedController = null; private ParticleSystem _smokeEffect = null; private TrailRenderer _tireTrail = null; private void Awake() { _attachedController = GetComponentInParent(); _smokeEffect = GetComponent(); _tireTrail = GetComponent(); _tireTrail.emitting = false; _tireTrail.startWidth = _attachedController.SkidWidth; _tireTrail.time = _attachedController.SkidDuration; } private void OnEnable() { _tireTrail.enabled = true; } private void OnDisable() { _tireTrail.enabled = false; } private void FixedUpdate() { if (_attachedController.Running) { if (_attachedController.Grounded) { _tireTrail.emitting = Mathf.Abs(_attachedController.LocalVelocity.x) > 10f; } else { _tireTrail.emitting = false; } if (_tireTrail.emitting) { if (_driftEffect != null) _driftEffect.SetActive(true); if (_smokeEffect != null) _smokeEffect.Play(); } else { if (_driftEffect != null) _driftEffect.SetActive(false); if (_smokeEffect != null) _smokeEffect.Stop(); } } else { _tireTrail.emitting = false; if (_smokeEffect != null) _smokeEffect.Stop(); if (_driftEffect != null) _driftEffect.SetActive(false); } } } //using UnityEngine; // public class SkidMarks : MonoBehaviour // { // [SerializeField] private GameObject driftParticles; // private VehicleController _controller = null; // private ParticleSystem _smoke = null; // private TrailRenderer _skidMark = null; // private void Awake() // { // _controller = GetComponentInParent(); // _smoke = GetComponent(); // _skidMark = GetComponent(); // _skidMark.emitting = false; // _skidMark.startWidth = _controller.SkidWidth; // _skidMark.time = _controller.SkidTime; // } // private void OnEnable() => _skidMark.enabled = true; // private void OnDisable() => _skidMark.enabled = false; // private void FixedUpdate() // { // if (_controller.CanRun) // { // if (_controller.IsGrounded) // _skidMark.emitting = Mathf.Abs(_controller.CarVelocity.x) > 10f; // else // _skidMark.emitting = false; // if (_skidMark.emitting) // { // if (driftParticles != null) driftParticles.SetActive(true); // if (_smoke != null) _smoke.Play(); // } // else // { // if (driftParticles != null) driftParticles.SetActive(false); // if (_smoke != null) _smoke.Stop(); // } // } // else // { // _skidMark.emitting = false; // if (_smoke != null) _smoke.Stop(); // if (driftParticles != null) driftParticles.SetActive(false); // } // } // }