- Home /
Question by
Osrro · May 23 at 01:51 PM ·
spawningspawning problems
I want the coins to spawn away from the player
im trying to make the coins spawn far from the player because sometimes they spawn inside the player and if the player exitTrigger with the coin the player dies
this is the spawner script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Spawner : MonoBehaviour { public GameObject coin; public GameObject player; public int coinCount;
void Start()
{
SpawnCoin(1);
}
void Update()
{
coinCount = FindObjectsOfType<Coin>().Length;
if (GameObject.Find("Player") != null)
{
if (coinCount == 0)
{
SpawnCoin(1);
}
}
else
{
SpawnCoin(0);
}
}
public void SpawnCoin(int coinToSpawn)
{
for (int i = 0; i < coinToSpawn; i++)
{
Instantiate(coin, GenerateSpawnPosition(), coin.transform.rotation);
}
}
public Vector3 GenerateSpawnPosition()
{
Vector3 playerPosition = player.transform.position;
float[] randomPos = { -1.5f, -1.6f, -1.7f, -1.8f, 1.5f, 1.6f, 1.7f, 1.8f };
var positionX = Random.Range(0, randomPos.Length);
var positionY = Random.Range(0, randomPos.Length);
Vector3 randomPosition = new Vector3(randomPos[positionX], randomPos[positionY], 0);
return randomPosition;
}
}
playerPosition isn't really doing anything i just typed it and trying to fix it
Comment