Unity Mathf.Lerp выполняет только те - PullRequest
0 голосов
/ 06 июня 2018
void Fire(float firingRate)
{
    TimePerFrame += Time.deltaTime;
    if(TimePerFrame >= firingRate)
    {
        Vector3 ProjectileDistance = new Vector3(0, 30, 0); //distance between center of the campion and it's head
        GameObject beam = Instantiate(projectile, transform.position + ProjectileDistance, Quaternion.identity) as GameObject;
        beam.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);
        //  AudioSource.PlayClipAtPoint(fireSound, transform.position);
        TimePerFrame = 0;
    }
}

void Update ()
{
    if (freezePosition == false)
    {
        Fire(firingRate);
        PositionChaning();
        firingRate = Mathf.Lerp(minFiringRate, maxFiringRate, 0.1f);
        Debug.Log(firingRate);
    }
}

Я хочу, чтобы моя скорость стрельбы была гибкой, я хочу, чтобы она начиналась с быстрой стрельбы и позволяла автоматически снижать скорость стрельбы.(чем больше значение floatRate, тем медленнее скорость)

Проблема в том, что firingRate = Mathf.Lerp(minFiringRate, maxFiringRate, 0.1f); срабатывает один и только один раз.Кажется, он не меняет своего значения в каждом кадре.

Debug.Log(firingRate); сообщает значение каждому кадру, но, похоже, он остается постоянным.

Почему это происходит?

Ответы [ 2 ]

0 голосов
/ 06 июня 2018

Ваша проблема здесь:

firingRate = Mathf.Lerp(minFiringRate, maxFiringRate, 0.1f);

Как видите, здесь Ваш t должен быть рассчитан для каждого кадра.

public static float Lerp(float a, float b, float t);

Вы можетеизмените его следующим образом:

private float fireTimer = 1.0f;
public float fireLimiter = 0.05f;

void Fire(float firingRate)
{
    TimePerFrame += Time.deltaTime;
    if(TimePerFrame >= firingRate)
    {
        Vector3 ProjectileDistance = new Vector3(0, 30, 0); //distance between center of the campion and it's head
        GameObject beam = Instantiate(projectile, transform.position + ProjectileDistance, Quaternion.identity) as GameObject;
        beam.GetComponent<Rigidbody2D>().velocity = new Vector3(0, projectileSpeed, 0);
        //  AudioSource.PlayClipAtPoint(fireSound, transform.position);
        TimePerFrame = 0;
    }
}

void Update ()
{
    if (freezePosition == false)
    {
        if(fireTimer > 0.0f){
            fireTimer -= Time.deltaTime * fireLimiter;
        }
        Fire(firingRate);
        PositionChaning();
        firingRate = Mathf.Lerp(minFiringRate, maxFiringRate, fireTimer);
        Debug.Log(firingRate);
    }
}

Не забудьте сбросить fireTimer на 1.0f после съемки!Задержка скорости огня может контролироваться с помощью fireLimiter

0 голосов
/ 06 июня 2018

Обновление запускает каждый кадр, как и ваш Mathf.Lerp Однако вы не меняете интерполяцию, которая в вашем случае определяется как 0.1f.

Изменяя эту интерполяцию, вы сможете добиться «сдвига» скорострельности.

В вашем случае вы можете определить переменную t вне области вашего обновления, иобновите его внутри Update() до t += 0.5f * Time.deltaTime;

Документация Mathf.Lerp содержит довольно хороший пример того, как это сделать

void Update()
{
    // animate the position of the game object...
    transform.position = new Vector3(Mathf.Lerp(minimum, maximum, t), 0, 0);

    // .. and increate the t interpolater
    t += 0.5f * Time.deltaTime;

    // now check if the interpolator has reached 1.0
    // and swap maximum and minimum so game object moves
    // in the opposite direction.
    if (t > 1.0f)
    {
        float temp = maximum;
        maximum = minimum;
        minimum = temp;
        t = 0.0f;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...