1
\$\begingroup\$

I am making my first visual effects, and I make effect, when my ship is flying. So I am want to play it only when the ship is actually moving. I would suppose it's easy, you call Play(), it will play. You play Stop() it will stop. But when I call play, nothing happens.

public void Update()
{
    _ship.Rotate(Input.GetAxis("Horizontal"));

    var move = Input.GetAxis("Vertical");
    _ship.Move(move);
    if (move > 0)
    {
        FlyEffect.Play();
        FlyEffect.SetVector3("Velocity", new Vector3(0, -_ship.Speed * move, 0));
    }
    else
    {
        FlyEffect.Stop();
    }
}

When I let in Initial Event Name OnPlay, it is playing as it should be. But nothing from the script... What I am missing here?

\$\endgroup\$
1
  • \$\begingroup\$ Have you tried calling Play() only on the frame when the effect should start and not every frame? \$\endgroup\$
    – DMGregory
    Commented Sep 10, 2020 at 17:21

1 Answer 1

2
\$\begingroup\$

Ok... At first I was expecting there is some inner attribute on VisualEffect, which will tell me if it's playing or not... But didn't find anything in that way. So then I was thinking it is not so much big problem, when you just call play...

But apparently is... As @DMGregory suggests, I just call the Play() once, added custom variable to hold state, if it is playing.

public void Update()
{
    _ship.Rotate(Input.GetAxis("Horizontal"));

    var move = Input.GetAxis("Vertical");
    _ship.Move(move);
    if (move > 0)
    {
        if (!_isPlaying)
        {
            FlyEffect.Play();
            _isPlaying = true;
        }
        FlyEffect.SetVector3("Velocity", new Vector3(0, -_ship.Speed * move, 0));
    }
    else
    {
        if (_isPlaying)
        {
            FlyEffect.Stop();
            _isPlaying = false;
        }
    }
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .