Search Unity

Changing Particle Direction While In Game?

Discussion in 'Scripting' started by MichaelHotte, Mar 1, 2015.

  1. MichaelHotte

    MichaelHotte

    Joined:
    Feb 22, 2015
    Posts:
    31
    So I've attached a particle system to the player in my game. When the player moves I would like the particles to emit in the opposite direction that the player is moving. For example, if the player was going up, the particles would be emitting to the south. This would give it an appearance of the player being propelled by the particles. Any idea on how this would be done? I know how to add a particle system and play around with it. Though I am not sure as to how I would create this in C# or Javascript. Thanks in advance.
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    245
    Hey, this link shows how to change the velocity of the particles
    http://answers.unity3d.com/questions/352554/particle-system-change-the-velocity-by-script.html

    As for movement direction, you can use an old position variable and subtract from the current position:

    Code (CSharp):
    1. void Update(){
    2. //Set up old position
    3. Vector3 oldPosition = transform.position;
    4.  
    5. //Move unit
    6. transform.position = blahblah
    7.  
    8. //Get direction the unit is moving
    9. Vector3 direction = (oldPosition - transform.position).normalized;
    10.  
    11. // set particle velocity here
    12. // (see above link for code)
    13.  
    14. }