Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 156 characters in body
Source Link

I'm making an endless runner game.

I have the script that make coins and walls appear at the same time in front of the player.

The problem is that sometimes the coin and wall appear in the same place/position.

How can I ensure that coins and walls do not appear in the same place/position?

public class Instantiate : MonoBehaviour
{
    public float betweenSpawn = 1.05f;
    public float angle;
    public Vector3 spawnValues; //Range of appearing 
    public GameObject coinPrefab;
    public GameObject wallPrefab;

    public Transform pathPointInstantiate;coinInstantiate;
    public Transform platformInstantiate;wallInstantiate;

    void Start()
    {
        InvokeRepeating("Spawn", 0f, betweenSpawn);
    }

    void Spawn()
    {
        Vector3 spawnPathPointPositioncoinPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
        Vector3 spawnPlatformPositionwallPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

        Instantiate(coinPrefab, spawnPathPointPositioncoinPosition + pathPointInstantiatecoinInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
        Instantiate(wallPrefab, spawnPlatformPositionwallPosition + platformInstantiatewallInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
    }
}

}

I'm making an endless runner game.

I have the script that make coins and walls appear at the same time in front of the player.

The problem is that sometimes the coin and wall appear in the same place/position.

How can I ensure that coins and walls do not appear in the same place/position?

public class Instantiate : MonoBehaviour
{
    public float betweenSpawn = 1.05f;
    public float angle;
    public Vector3 spawnValues; //Range of appearing 
    public GameObject coinPrefab;
    public GameObject wallPrefab;

    public Transform pathPointInstantiate;
    public Transform platformInstantiate;

    void Start()
    {
        InvokeRepeating("Spawn", 0f, betweenSpawn);
    }

    void Spawn()
    {
        Vector3 spawnPathPointPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
        Vector3 spawnPlatformPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

        Instantiate(coinPrefab, spawnPathPointPosition + pathPointInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
        Instantiate(wallPrefab, spawnPlatformPosition + platformInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
    }
}

I'm making an endless runner game.

I have the script that make coins and walls appear at the same time in front of the player.

The problem is that sometimes the coin and wall appear in the same place/position.

How can I ensure that coins and walls do not appear in the same place/position?

public class Instantiate : MonoBehaviour
{
public float betweenSpawn = 1.05f;
public float angle;
public Vector3 spawnValues; //Range of appearing 
public GameObject coinPrefab;
public GameObject wallPrefab;

public Transform coinInstantiate;
public Transform wallInstantiate;

void Start()
{
    InvokeRepeating("Spawn", 0f, betweenSpawn);
}

void Spawn()
{
    Vector3 coinPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
    Vector3 wallPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

    Instantiate(coinPrefab, coinPosition + coinInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
    Instantiate(wallPrefab, wallPosition + wallInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
}

}

Formatting, clarity
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

Instantiate two difference objects on differentat random non-overlapping positions

I'm new in scripting. Before scripting, I just painting. Now I have little problem with coding. I'm making aan endless runner andgame.

I have the script that make coins and walls appear at the same time in front of the player. But

The problem is that sometimes the coin and wall appear in the same place/position. 

How to docan I ensure that coins and walls appeardo not appear in the same place/position?

public class Instantiate : MonoBehaviour
{
    public float betweenSpawn = 1.05f;
    public float angle;
    public Vector3 spawnValues; //Range of appearing 
    public GameObject coinPrefab;
    public GameObject wallPrefab;

    public Transform pathPointInstantiate;
    public Transform platformInstantiate;

    void Start()
    {
        InvokeRepeating("Spawn", 0f, betweenSpawn);
    }

    void Spawn()
    {
        Vector3 spawnPathPointPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
        Vector3 spawnPlatformPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

        Instantiate(coinPrefab, spawnPathPointPosition + pathPointInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
        Instantiate(wallPrefab, spawnPlatformPosition + platformInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
    }
}

Instantiate two difference objects on different positions

I'm new in scripting. Before scripting, I just painting. Now I have little problem with coding. I'm making a runner and I have the script that make coins and walls appear at the same time in front of the player. But problem is that sometimes coin and wall appear in same place/position. How to do that coins and walls appear not in same place/position?

public class Instantiate : MonoBehaviour
{
public float betweenSpawn = 1.05f;
public float angle;
public Vector3 spawnValues; //Range of appearing 
public GameObject coinPrefab;
public GameObject wallPrefab;

public Transform pathPointInstantiate;
public Transform platformInstantiate;

void Start()
{
    InvokeRepeating("Spawn", 0f, betweenSpawn);
}

void Spawn()
{
    Vector3 spawnPathPointPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
    Vector3 spawnPlatformPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

    Instantiate(coinPrefab, spawnPathPointPosition + pathPointInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
    Instantiate(wallPrefab, spawnPlatformPosition + platformInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
}
}

Instantiate two objects at random non-overlapping positions

I'm making an endless runner game.

I have the script that make coins and walls appear at the same time in front of the player.

The problem is that sometimes the coin and wall appear in the same place/position. 

How can I ensure that coins and walls do not appear in the same place/position?

public class Instantiate : MonoBehaviour
{
    public float betweenSpawn = 1.05f;
    public float angle;
    public Vector3 spawnValues; //Range of appearing 
    public GameObject coinPrefab;
    public GameObject wallPrefab;

    public Transform pathPointInstantiate;
    public Transform platformInstantiate;

    void Start()
    {
        InvokeRepeating("Spawn", 0f, betweenSpawn);
    }

    void Spawn()
    {
        Vector3 spawnPathPointPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
        Vector3 spawnPlatformPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

        Instantiate(coinPrefab, spawnPathPointPosition + pathPointInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
        Instantiate(wallPrefab, spawnPlatformPosition + platformInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
    }
}
Source Link

Instantiate two difference objects on different positions

I'm new in scripting. Before scripting, I just painting. Now I have little problem with coding. I'm making a runner and I have the script that make coins and walls appear at the same time in front of the player. But problem is that sometimes coin and wall appear in same place/position. How to do that coins and walls appear not in same place/position?

public class Instantiate : MonoBehaviour
{
public float betweenSpawn = 1.05f;
public float angle;
public Vector3 spawnValues; //Range of appearing 
public GameObject coinPrefab;
public GameObject wallPrefab;

public Transform pathPointInstantiate;
public Transform platformInstantiate;

void Start()
{
    InvokeRepeating("Spawn", 0f, betweenSpawn);
}

void Spawn()
{
    Vector3 spawnPathPointPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
    Vector3 spawnPlatformPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));

    Instantiate(coinPrefab, spawnPathPointPosition + pathPointInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));
    Instantiate(wallPrefab, spawnPlatformPosition + platformInstantiate.TransformPoint(0, 0, 0), Quaternion.AngleAxis(angle, Vector3.forward));    
}
}