Hello, i would like to know how i could change the particles local velocity through code
here’s the code below
var healthBar = new ParticleEmitter();
healthBar.particleEmitter.localVelocity.x=(maxVel*(playerHealth/100));
receiving this error
Assets/TuorialAssets/Material/05-player/health.cs(44,37): error CS1612: Cannot modify a value type return value of `UnityEngine.ParticleEmitter.localVelocity’. Consider storing the value in a temporary variable
Try using this, instead-
Vector3 originalVelocity = healthBar.particleEmitter.localVelocity;
float newX = maxVel*(playerHealth/100);
healthBar.particleEmitter.localVelocity = new Vector3(newX, originalVelocity.y, originalVelocity.z);
This happens because you’re not allowed to modify the x, y and z components of the value directly. You have to create an entirely new Vector3!
From the filename, I assume you are using C#- why do you have a ‘var’ variable? You should use strong typing whenever you can- also, you shouldn’t use the constructor to create components:
ParticleEmitter healthBar = gameObject.AddComponent<ParticleEmitter>();
then, just use
healthBar.localVelocity;
instead of
healthBar.particleEmitter.localVelocity;