0
\$\begingroup\$

I am making a simple game with a ball that moves left and right when it hits a wall. I want the game to be like an arcade type game where the ball is on a platform and it keeps jumping up.

I've written code to spawn a list of prefabs randomly stacked vertically with a fixed gap. Since this is an endless runner, I want the bottom Prefabs to be destroyed and new one to be spawned on top of the previous one.

But when a new prefab is being spawned, sometimes the gap made on the y axis is off by a decimal point, and the more you play, the bigger the gap is, until the ball can't make the jump to the next platform.

Any Solutions?

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PlatformSpawner : MonoBehaviour
{
    public GameObject[] prefabs;
    private GameObject platform;
    public float gap = 3f; 
    public int maxInstances = 20; 
    
    private int numInstances = 0;

    public ArcadeMovement scoreCountScript;

    private float spawnerPosition = 0f;
    private float lastSpawnedPosition = 0f;

    private GameObject newPrefab;

    private void Start()
    {
        spawnerPosition = transform.position.y;
    }
    void Update()
    {
        float scoreCounter = scoreCountScript.scoreCount;

        Vector3 currentPosition = transform.position;
        Transform currentRotation = gameObject.transform;
        currentPosition.x = 0f;
        currentRotation.eulerAngles = new Vector3(currentRotation.eulerAngles.x, currentRotation.eulerAngles.y, 0f);
        transform.position = currentPosition;

        if (numInstances < maxInstances)
        {
            int index = 0;
            if (scoreCounter < 25)
            {
                index = Random.Range(0, 3);
            }

            else if (scoreCounter >= 25 && scoreCounter < 50)
            {
                index = Random.Range(0, 5);
            }

            else if (scoreCounter >= 50)
            {
                index = Random.Range(0, 7);
            }

            currentPosition = transform.position + new Vector3(0f, Mathf.RoundToInt(numInstances * gap), 0f);
            newPrefab = Instantiate(prefabs[index], currentPosition, Quaternion.identity);
            currentPosition += newPrefab.transform.position;
            numInstances++;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name.StartsWith("Platform"))
        {
            Destroy(collision.gameObject);
            Debug.Log("Platform Destroyed");
            numInstances--;
        }
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ You are using Mathf.RoundToInt - can it be that your platform is a tiny bit below or above a full units worth height? They would all allign until at one point it gets rounded the other way and the gap would be way bigger. And what is affected by transform.position when you set it? Is the spawner moving along the player? \$\endgroup\$ Commented Apr 24, 2023 at 10:16
  • \$\begingroup\$ @Zibelas Yeah so the spawner starts right below the player. So the first prefab spawned will be where the player first starts. And I've set a collider as a trigger with an offset of -3 on the y axis. Also given a script for the spawner that moves with the player on the y axis only because I want the platforms to be formed straight. \$\endgroup\$ Commented Apr 24, 2023 at 12:24
  • \$\begingroup\$ Please still double check the dimension of the actual platform that you are spawning \$\endgroup\$ Commented Apr 24, 2023 at 13:24
  • \$\begingroup\$ There's a lot of seemingly 'random' nonfunctional or confusingly useless code in here. I'd recommend cleaning things up, as it might help you find the solution. For instance, spawnerPosition seems unused. The assignment and usage of currentPosition seems transient and simply produces confusion. I don't see why it's being added/set to at the end if it's just going to be discarded afterwards, but maybe this is causing some reference modification with the current position that would break things? Either way, clean up is direly needed here. \$\endgroup\$ Commented Apr 24, 2023 at 13:28
  • \$\begingroup\$ @Zibelas thanks for pointing that out. Finally got the issue fixed its all goin smoothly now. :) \$\endgroup\$ Commented Apr 25, 2023 at 8:20

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.