Мой PowerUp не будет работать, если я установлю более одного - PullRequest
0 голосов
/ 13 декабря 2018

Привет, я сделал скрипт для моей игры.Он предназначен для повышения игрока, и это происходит, но когда я ставлю более одного бонуса, игрок может столкнуться с ним не работает.Я даже переписал весь сценарий, поэтому не использовал статическую переменную, потому что думал, что это проблема, но, видимо, нет.Он отлично работает, когда у меня есть один powerup на карте, но когда я размещаю два или более, он не работает, но все равно печатает мой файл debug.log на консоль.

Код:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUp : MonoBehaviour {

    public bool boosting = false;
    public GameObject effect;
    public AudioSource clip;


    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            if (!boosting)
            {
                //Audio play
                clip.Play();
                //Start effect at powerups position
                GameObject explosion = Instantiate(effect, transform.position, transform.rotation);
                Destroy(explosion, 2);

                //disable collider and meshrender
                GetComponent<MeshRenderer>().enabled = false;
                GetComponent<Collider>().enabled = false;

                //Get the tankmovementscript via the collider and increase the speed
                TankMovement collidedMovement = other.gameObject.GetComponent<TankMovement>();
                collidedMovement.m_Speed = 20f;

                boosting = true;
                Debug.Log(boosting);
                StartCoroutine(coolDown());
            }
        }
        else if(other.gameObject.CompareTag("Player1"))
        {
            if(!boosting)
            {
            //Audio play
            clip.Play();

            //Start effect at powerups position
            GameObject explosion =Instantiate(effect, transform.position, transform.rotation);
            Destroy(explosion, 2);

            //disable collider and meshrender
            GetComponent<MeshRenderer>().enabled = false;
            GetComponent<Collider>().enabled = false;

            //Get the tankmovementscript via the collider and increase the speed
            TankMovement1 collidedMovement = other.gameObject.GetComponent<TankMovement1>();
            collidedMovement.m_Speed1 = 20f;

            boosting = true;
            Debug.Log(boosting);
            StartCoroutine(coolDown());
            }
        }
    }

    private IEnumerator coolDown()
    {
        if (boosting == true)
        {
            yield return new WaitForSeconds(4);
            {
                boosting = false;

                //wait 4 seconds enable the powerup again
                GetComponent<MeshRenderer>().enabled = true;
                GetComponent<Collider>().enabled = true;
                Debug.Log(boosting);


            }
        }
    }

     void reset()
     {
        //again find both tanks with name and get their movement script variable and change it back to normal
        GameObject.Find("Tank(Clone)").GetComponent<TankMovement>().m_Speed = 12f;
        GameObject.Find("Tank1(Clone)").GetComponent<TankMovement1>().m_Speed1 = 12f;

    }

    private void Update()
    {  //checking to see if the player is not boosting set speed to normal
        if (!boosting)
        {
            reset();
        }

    }   

}

1 Ответ

0 голосов
/ 13 декабря 2018

Вы упомянули поведение просто из-за функции «Сброс», когда у вас более двух усилений, другие отключают усиление и устанавливают скорость проигрывателя на ноль.

Есть разные подходы к этомупроблема, одно объявляет «усиление» поля статическим, так что оно будет общим свойством для всех бонусов, но у него будут свои побочные эффекты, такие как эффекты не будут складываться должным образом;

, так что у вас естьреализовать простую систему эффектов, чтобы игрок мог складывать эффекты бонусов, и ответственность за усиление будет заключаться только в доставке этих эффектов, а не в их активации / деактивации.

простая реализация может выглядеть так:

эффекты:

public interface IEffect
{
    float Duration { get; set; }
    void Do(Player p);
    void Undo(Player p);
}

public class ActiveEffect
{
    public IEffect Effect { get; private set; }
    public float LeftTime;
    public ActiveEffect(IEffect effect)
    {
        Effect = effect;
        LeftTime = effect.Duration;
    }
}

public class SpeedBoost : IEffect
{
    public float Duration { get; set; }

    public SpeedBoost()
    {
        Duration = 4;
    }
    public void Do(Player p)
    {
        p.speed = 40f;
    }

    public void Undo(Player p)
    {
        p.speed = 20f;
    }
}

ваш менеджер эффектов:

public class EffectManager
{
    Dictionary<Type, ActiveEffect> _activeEffects = new Dictionary<Type, ActiveEffect>();
    public void AddEffect(IEffect effect)
    {
        var type = effect.GetType();
        if (_activeEffects.ContainsKey(type))
        {
            //reseting effect duration to max
            _activeEffects[type].LeftTime = effect.Duration;
        }
        else
        {
            var activeEffect = new ActiveEffect(effect);
            _activeEffects.Add(type, activeEffect);
            StartCoroutine(activeEffect);
        }
    }

    private IEnumerator Apply(ActiveEffect effect)
    {
        effect.Effect.Do(player);
        while (effect.LeftTime > 0)
        {
            yield return new WaitForEndOfFrame();
            effect.LeftTime -= Time.deltaTime;
        }
        effect.Effect.Undo(player);
        _activeEffects.Remove(effect.Effect.GetType());
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...