- Home /
Particle system rate by speed of vehicle
Hello, First thanks for your time reading.
what I want is; Smoke out of the engine of the car via partycle system. When moving faster I want more smoke ( higher emission rate)
I tried:
using UnityEngine;
using System.Collections;
public class CarSound : MonoBehaviour {
public float topSpeed = 100; // km per hour
private float currentSpeed = 0;
public ParticleSystem.EmissionModule emission;
void Update () {
currentSpeed = transform.GetComponent <Rigidbody> ().velocity.magnitude * 3.6f;
emission = currentSpeed / topSpeed;
transform.GetComponent <ParticleSystem> ().emission = emission;
}
}
I cant test it because I get few errors:
Particle.emission cannot be assigned to 'it is read only'.
I cant fix it my self. I am still a beginner and I don't know that much!
Anyone with a solution ?
Kind regards, Ranec
Answer by Mister-Mortal · Nov 22, 2016 at 02:38 PM
Just grab a reference to emission object and change emission value in that reference. Like this:
var emissionController = gameObject.GetComponent<ParticleSystem>().emission;
emissionController .rateOverTime = emission;
Update:
Misread your script a bit. Here's the solution that works:
public class CarSound : MonoBehaviour {
public float topSpeed = 100; // km per hour
private float currentSpeed = 0;
public ParticleSystem particleSystem;
void Update()
{
currentSpeed = transform.GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
float emissionAmount = currentSpeed / topSpeed;
var emissionController = particleSystem.emission;
emissionController.rateOverTime = emissionAmount;
}
}
Thank you for your reply, but that didn't help unfortunately i'm sorry.
Sorry mate, misread your script. Updated answer with solution that should work.
Thanks @$$anonymous$$ister-$$anonymous$$ortal,
emissionController.rateOverTime = emissionAmount;
changed to
emissionController.rate = emissionAmount;
Cause rateOverTime does not work for me tho. but .rate did.
Anyways it worked like a charm. Thank you for your time.