- Home /
Change particle velocity when gameobject rotates
Im trying to create a little game with a space ship and encountered a small problem with trail of space ship engine. I've created a particle and wrote a small script which increase start lifetime and size of particles to produce visual effect of ship speed increasing. But, when my ship rotates I want to show that trail of ship rotates slowly also (currently my trail always looks like straight line and i want to modify it so it will look like curve when my ship rotates). So, I have several variables from my "movement script": speed of ship, vector to target and angel between ship and target. After using google I found a way to change velocity of particle with the help of next code:
public void UpdateDirection()
{
float distance = 5;
ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particle.particleCount];
int count = particle.GetParticles(particles);
for(int i = 0; i < count; i++)
{
float xVel = (particles[i].lifetime / particles[i].startLifetime) * distance;
particles[i].velocity = new Vector3(xVel, 0, 0.2f);
}
particle.SetParticles(particles, count);
}
I've tried several diff. ways of modifying velocity with my vector.x coordinate, angel and etc, but after this my particle worked very strange way, like without any lifetime, or just bursting in left or right side. How can I use my data to modify velocity and move from straight line to curve trail? Pls help :(
EDIT: Excuse me to bringing back this question, but after sometime I realised that simulation space isnt helping me with that problem. Currently it rotates right at the emit point and this is bad, because it looks ugly. I need some way of applying force to particle after sometime, like script option of "force over lifetime" (I cant use this direct from unity editor, because rotating is random)
To get the trail to curve modify the particles angularVelocity.
It sounds like a lot of what you are doing can be done using the ParticleSystem without code.
For example the Size By Speed and Color By Speed tabs could help replace your current method. Set the simulation space to be world. Set inherit velocity to 1.
I cant mark you comment as answer, so just thx you! :p Need to rework my particle system after that, but they look like curve now.
Answer by hexagonius · Feb 23, 2015 at 07:58 AM
It's definitely simulation space world as karljj1 said. But inherit velocity should be 0. You don't need to script anything for that
Err, after some homework I realised that this is not helping me with problem. Can i somehow script "force over lifetime" behavior?
In the unity editor I can edit "force over lifetime" parameter in particle editor. Changing "X" coordinate giving me desired effect. But i cant set fixed "X" coordinate from editor, I need to change it from the game, so I looking for a way to write a script, so i can change it from game, but no idea how I can change this parameter from script..
I'm not sure I follow. You want the x coordinate to stay the same but the y and z to change over time? Could you do it using the curve setting ins$$anonymous$$d of constant?
I've added a picture with my problem. Top pictures are my ideal representation of what I want from my particle system and bottom one is what I currently have. Problem is - I want to change X coordinate after some delay if my ship is rotating (like on the top pictures), but idk how to do this. World simulation space result does not provide me any delay :(
Answer by Digital-Phantom · Feb 23, 2015 at 08:08 AM
If you use the built in particle system there is an option to toggle between LocalSpace / WorldSpace. I think its WorldSpace you need (if not choose the other) this will give your particles the effect of moving/swinging around as your objects rotation changes
:)
Your answer
![](https://koobas.hobune.stream/wayback/20220613184020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Particle collider not working 0 Answers
An OS design issue: File types associated with their appropriate programs 1 Answer