Efecto visual de Unity: Play () no tiene ningún efecto

Sep 11 2020

Estoy haciendo mis primeros efectos visuales, y hago efecto, cuando mi barco está volando. Así que quiero jugarlo solo cuando el barco se esté moviendo. Supongo que es fácil, llama a Play (), se reproducirá. Juegas Stop () se detendrá. Pero cuando llamo al juego, no pasa nada.

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();
    }
}

Cuando dejo entrar el nombre del evento inicial OnPlay, se está reproduciendo como debería ser. Pero nada del guión ... ¿Qué me falta aquí?

Respuestas

2 Zechy Sep 11 2020 at 00:30

Ok ... Al principio esperaba que hubiera algún atributo interno activado VisualEffect, que me dirá si está jugando o no ... Pero no encontré nada de esa manera. Entonces estaba pensando que no es un gran problema, cuando simplemente llamas al juego ...

Pero aparentemente es ... Como sugiere @DMGregory, simplemente llamo a Play () una vez, agregué una variable personalizada para mantener el estado, si se está reproduciendo.

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;
        }
    }
}