Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting Particle System velocity to face heading

Discussion in 'Physics' started by MadboyJames, Dec 11, 2019.

  1. MadboyJames

    MadboyJames

    Joined:
    Oct 28, 2017
    Posts:
    262
    I am trying to use the particle system to launch projectiles. If I set the VelocityOverLifetime module to z of 10, in local space (the game is top-down), then it fires from my forward position. If I turn too fast, then all the bullets I fired suddenly change velocity and come back at me!

    My solution is changing the space to world, and making a script called ParticlesFaceLaunchDirection, which will set the x and y values of the VelocityOverLifetime based on speed and current z angle.

    This is my trigonometry

    float angle = transform.rotation.z * Mathf.Deg2Rad;
    Vector2 velocity= new Vector2(Mathf.Cos(angle),Mathf.Sin(angle));
    Debug.Log(Mathf.Cos(angle));


    The problem is that the Cos and Sin functions return extremely small values, causing my emitters to hardly change direction, let alone actually be facing forward. Is there something I have to do to my angle or velocity variables before setting the values in the velociyOverLifetime module?

    UPDATE:

    In the end I used
     Vector2 velocity = Quaternion.Euler(0, 0, transform.rotation.eulerAngles.z) * Vector2.right;
    , and I experienced the same behavior as I did with local space: changing the rotation changed the velocity of already launched particles. Currently I have no working solution, any help is appreciated.
     
    Last edited: Dec 12, 2019