Search Unity

Cannot aim particle system

Discussion in '2D' started by turp182, Jul 17, 2018.

  1. turp182

    turp182

    Joined:
    May 3, 2018
    Posts:
    52
    I'm asking how to set the shape.rotation on a ParticleSystem in 2D. I've tried setting the z value directly a few ways and then tried the rotation.Set method to no avail (directly and also from a variable as in the example below).

    It would seem that this should be straightforward. If I change the shape.rotation in the editor it works as expected. I just can't get the rotation change to "stick" in code.

    Class Level Fields

    public SpriteRenderer renderer;
    private SpriteRenderer parentRenderer;
    private ParticleSystem particles;


    void Start() Initialization Code

    List<SpriteRenderer> spriteRenderers = GetComponents<SpriteRenderer>().ToList();
    renderer = spriteRenderers.Where(b => b.name == this.name).FirstOrDefault(); // there is only one and it resolves correctly
    particles = renderer.GetComponent<ParticleSystem>();


    Attempting to set the shape.rotation on the particles in FixedUpdate():

    // determine angle, adjust for negative values
    var p1 = particles.transform.position;
    var p2 = PlayerCharacter.PlayerInstance.transform.position;

    float angleToPlayer = Mathf.Atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Mathf.PI;
    angleToPlayer = angleToPlayer > 0 ? angleToPlayer : angleToPlayer + 360f;

    var shape = particles.shape;
    shape.rotation.Set(0, 0, angleToPlayer);

    var test = particles.shape.rotation; // always 0,0,0 - expecting Z value to be set to the value of angleToPlayer

    particles.Emit(1);


    I will heap praises upon whoever can answer this. Took long enough to get the angle stuff working correctly (there's a Raycast2D that works but is not in the code example). I have a lot of C# experience, but Unity doesn't always function as I would expect.

    Thanks,
    turp182
     
    Last edited: Jul 17, 2018
  2. turp182

    turp182

    Joined:
    May 3, 2018
    Posts:
    52
    More info. Here's a screen shot where I have pinned the shape.rotation value (0,0,45 - default value on the prefab). It also shows the angleToPlayer at 267 degrees. Finally, the execution point (yellow line) is past the shape.rotation.Set(...) call.

    upload_2018-7-17_18-3-28.png

    Here's some of the particle settings (in flux, focusing on scripting first):
    upload_2018-7-17_18-5-43.png upload_2018-7-17_18-6-16.png upload_2018-7-17_18-6-33.png
     
  3. turp182

    turp182

    Joined:
    May 3, 2018
    Posts:
    52
    I figured it out, I had to create a variable for the Vector3 rotation, now it works:

    I can't figure out why there is a shape.rotation.Set method...

    Vector3 rotationVector = particles.shape.rotation;
    rotationVector.z = angleToPlayer;
    shape.rotation = rotationVector;