0

I'm so nooby at unity. I'm trying to make a flappy bird clone and ı wanna fasten pipes which are coming toward bird. ı can fasten them but in game scene there are still slower pipes and it doesnt seem good how can ı solve this problem? Sorry for my English its not my mother tongue.

void Update()
{
    if (firstTap && KlonlamaBirKereCalissin)
    {
        KlonlamaBirKereCalissin = false;
        InvokeRepeating(nameof(PipeClonin), 1, 6f); ==> in first tap it start to cloning
        
    }

      
    if (score > 10 && KlonlamaBirKereCalissin10) ==> checking if score 10 and if the answer is yes i cancell earlier invoke and started faster one
    {
     //   SlowPipes = GameObject.FindGameObjectsWithTag("Pipes"); ==> in here ı tried to faster last two slower pipes but ıt was wrong cos its update func and last two pipes are changing so the result is changing too


      //  SlowPipes[2].GetComponent<PipeMove>().PipeSpeed = 1.5f;
      //  SlowPipes[3].GetComponent<PipeMove>().PipeSpeed = 1.5f;
        CancelInvoke(nameof(PipeClonin));
        InvokeRepeating(nameof(PipeClonin), 1f, 5f);
        KlonlamaBirKereCalissin10 = false;
        pipeMoveScript.PipeSpeed = 1.5f;

2 Answers2

0

This sounds more like a game design challenge/decision.

To stop having the faster pipes catch up with slower ones, the only solution that I can think of is speeding them all up.

It's hard to tell what the code you've provided does as there's no context and it's incomplete. So here's a simple generic implementation answer based on what you've said.

Some object and a script on it would be in charge of controlling the pipes, say PipeController. This would have a method SpawnNew that handles instantiating new pipes, as well as adding them to a list, say pipes. Using this list, when you want to change the speed of all the pipes, you could just iterate over all the pipes and change their speed value.

public class PipeController : MonoBehaviour
{
    [SerializeField] private PipeMove _pipePrefab;
    private List<PipeMove> _pipes;

    private void Awake()
    {
        _pipes = new List<PipeMove>();
    }

    private void SpawnNew()
    {
        PipeMove pipeMove = Instantiate(_pipePrefab);
        _pipes.Add(pipeMove);

        // Set things like initial speed etc here
    }

    private void SetNewSpeed(float newSpeed)
    {
        foreach (PipeMove pipe in _pipes)
        {
            pipe.PipeSpeed = newSpeed;
        }
    }
}
0

You could simply make the PipeSpeed a static so all instances use the same globally shared value

public class PipeMove : MonoBehaviour
{
    public static float PipeSpeed = 1;

    ...
}

then your main game controller can set it for all instances via

PipeMove.PipeSpeed = 1.5f;

actually instead of the InvokeRepeating in your use case I would also rather use a Coroutine which can be based on the very same value so you control both, the spawn interval as well as he move speed with the same single field like e.g.

private IEnumerator Start()
{
    yield return new WaitUntil(() => firstTap);

    var nextCheckpoint = 10;
    while(true)
    {
        if(score >= nextCheckpoint)
        {
            // whatever formula you want to use to increase speed
            PipeMove.PipeSpeed *= 1.5f;

            nextCheckpoint += 10;
        }

        PipeClonin();

        yield return new WaitforSeconds(/*whatever formula you want*/ 1 / PipeMove.PipeSpeed  * 10f);
    }
}

This way you could even increase the speed gradually over time or score and not go in jumps at all.

If needed you could even make the score check run completely parallel to the spawning so that it also immediately increases speed if you hit the next score checkpoint while waiting for the next spawn

private IEnumerator Start()
{
    yield return new WaitUntil(() => firstTap);

    StartCoroutine(ScoreCheck());

    while(true)
    {
        PipeClonin();

        yield return new WaitforSeconds(/*whatever formula you want*/ 1 / PipeMove.PipeSpeed * 10f);
    }
}

private IEnuemrator ScoreCheck()
{
    var nextCheckpoint = 10;
    while(true)
    {
        if(score >= nextCheckpoint)
        {
            // whatever formula you want to use to increase speed
            PipeMove.PipeSpeed *= 1.5f;

            nextCheckpoint += 10;
        }

        yield return null;
    }
}

or even better than poll checking this actually only do this check whenever the score is actually changed.

derHugo
  • 83,094
  • 9
  • 75
  • 115