-1

So I am tryng to make a game in unity using C# code, now...I have a cube that jumps on pylons... It's an infinite runner game.

The problem is that I want to spawn the pylons based on the distance that the cube has traveled...

Let say that the cube has traveled 12 meters, I want to spawn a new pilon every time the cub has traveled that distance, so it traveled 12 meters, spawn a new pylon, it traveled another 12 meters, spawn a new pylon and so on...

I want to make the cub travel faster and faster so that why I need to know the distance at which the pylons need to spawn.

I tried to use the functions Instantiate(), and used an if statement looking for transform.position.z of the cube..

The real problem is that every time the cube has traveled 12 meters and is time to instantiate a new pylon, it spawns like 20 pylons because I use this in update function , and it sees the position as a float , instantiating the the full number, 12,1 ; 12,2 ; 12,3 ; 13,4 ... until ; 12,99 ... I want to know how can I spawn the pylon only once.

Also...if you have a better solution for spawning the pylons please show me.

  This is the code that I used to spawn the pylons ,respectively the functions that I used.
 Some of this are the variables that I used.

    public float cameraSpeed = 1;
    public float horizontalSpeed;
    private int spawnIndex;
    public float spawnDistance;
    private int[] prevPoints;
    public GameObject[] pilon;
    public GameObject spawnMainPoint;
    public Transform[] spawnPoints;
    public Transform[] coinsSpawnPoint;
    public float enamySpeed;
    private int currentPoint;
    public Transform[] pointPosition;

void FixedUpdate () {
    int currPosition = (int)transform.position.z;
    if (currPosition % spawnDistance == 0f) {
        SpawnCoinPylon ();
        SpawnNormalPylon ();
    }

void SpawnNormalPylon ()
    {
        spawnIndex = Random.Range (0, spawnPoints.Length);
        Instantiate (pilon[0], spawnPoints [spawnIndex].position,spawnPoints [spawnIndex] .rotation);

    }
    void SpawnCoinPylon(){
        Instantiate (pilon[1], coinsSpawnPoint [0].position,spawnPoints [spawnIndex] .rotation);
    }

Thank you!

UPDATE :

I managed to pull it of , here is the correctly working code:

private int currPosition ;

void Start () {
    currPosition = (int) transform.position.z;
}
void FixedUpdate () {
    bool hasSpawnedPylon = false;
    if (currPosition != (int)transform.position.z) {
        if ((int)transform.position.z % spawnDistance == 1) {
        if (!hasSpawnedPylon) {
            SpawnCoinPylon ();
                SpawnNormalPylon ();
                    currPosition = (int)transform.position.z;
        }
        }
    }
        else
        {
            hasSpawnedPylon = false;
        }
}

    void SpawnNormalPylon ()
{
    spawnIndex = Random.Range (0, spawnPoints.Length);
    Instantiate (pilon[0], spawnPoints [spawnIndex].position,spawnPoints [spawnIndex] .rotation); 

}
void SpawnCoinPylon(){
    Instantiate (pilon[1], coinsSpawnPoint [0].position,spawnPoints [spawnIndex] .rotation);
}
Dinu Adrian
  • 129
  • 2
  • 13
  • 1
    How about you just use time to spawn the pylons and simply make it faster the longer they player is alive. I think it would be much simpler to do. – I.B Apr 15 '17 at 22:28
  • 1
    Post the code that is spawning 20+ objects and someone here can help you figure out the problem. – Programmer Apr 15 '17 at 22:44
  • Thank you for your response. – Dinu Adrian Apr 15 '17 at 23:46
  • I am a little bit skeptical about using the time to spawn the pylons, I am concerned that if the speed will increase glitches like spawning the pylons to far on from another or other things like that will appear...I haven't tried that tho...I would prefer to do it in some other way I it is possible. If I have no other way I think I will have to try that but, as I say, I don't really like that ideea. – Dinu Adrian Apr 15 '17 at 23:46

1 Answers1

1

I hope this solves your problem.

    bool hasSpawnedPylon = false;
    void FixedUpdate()
    {
        int currPosition = (int)transform.position.z;
        if (currPosition % spawnDistance == 0f)
        {
            if (!hasSpawnedPylon)
            {
                SpawnCoinPylon();
                SpawnNormalPylon();
            }
        }
        else
        {
            hasSpawnedPylon = false;
        }
    }

Be aware that checking if floats are exactly equal to 0f is a bad practice and will result in errors that you would've not expected. It is generally better to compare it as (difference of two floats) < some small threshold values. This is because floats are often stored with minor differences when it comes to int values. For example, 5.0f is often stored as 0.4999f or 0.5001f.

Matt
  • 1,424
  • 1
  • 13
  • 17
  • Thank you for your help, your code did not actually worked but it helped to make the final result work. I updated the question so you guys can see what I did. – Dinu Adrian Apr 16 '17 at 07:16