Search Unity

Gradually changing the speed of a Shuriken system's particles...

Discussion in 'Scripting' started by Loius, Dec 6, 2012.

  1. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Ugh, math. Hate the stuff.

    I'm using a Shuriken ParticleSystem to represent a starfield. The camera doesn't move; the starfield is responsible for creating the illusion of movement. It's only moving along the X axis.

    To start with, the system has a startSpeed of 0.1 > 1.0, so I can easily scale it as necessary. They're also sized by speed, so slow stars appear farther away.

    Now, I need to go "from" my current X speed "to" my target X speed over some amount of time, T. You can't just add velocity to the particles to do this, you have to use some multiplication of some kind to get smooth motion (if you add velocity over time to get to the target, all the current particles fly off the screen as one).

    I have no idea what formula to use, or even what words to search for to try to find it.

    A more concrete example of my specific conundrum -
    Say I'm at speed = 3, and want to get to 5. I'll need to multiply by a value greater than one each frame (to speed up), and eventually arrive at 5/3 times my original speed. But to go from 5 to 3, I need a value less than one, to eventually arrive at 3/5 my original speed.

    I know Time.deltaTime needs to be in there somewhere, that's about it.
     
  2. originalbadboy

    originalbadboy

    Joined:
    Jul 3, 2012
    Posts:
    14
    You could just use an animation event and change the speed that way. Just set up some animations that change the speed of the particles and then call each one when the speed changes.

    Probably easier than trying to script it for now.
     
  3. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    (unless I'm misunderstanding) that can't be done. I need to apply the same overall change (5/3 or whatever) to speed and velocity - I can't access the system's velocity through code at all (Unity! Why are you HIDING Shuriken settings? I assume there's a darn good reason...), so I can't animate it. I can animate start speed, but I need the particle velocities to change by the same ratio. :(

    (I can change the individual particles' velocities in code with Get/Set particles, but I still need the formula to figure out what to change them by)

    edit - Here's where I"m tripping up on the math:

    Code (csharp):
    1. function GradualVelocityFromTo( from : Vector3, to : Vector3, time : float ) {
    2.     /*Muxstyle*/
    3.     /**/
    4.     var persec : Vector3 = (to/from) /*... ? ...*/;
    5.     while ( time > 0 ) {
    6.         VelocityMux( persec * Time.deltaTime );  // < can't do this!  deltaTime is too small
    7.         time -= Time.deltaTime;
    8.         yield;
    9.     }
    10.     /**/
    11.  
    If I just VelocityMux (mux is 'multiply') the velocity directly to (to/from), it works (but sends all the stars offscreen as a block / sends fast stars over the top of the existing ones). I need to make persec 'closer to one' by some amount related to Time.deltaTime. :S
     
    Last edited: Dec 6, 2012