How to add my coin to parallax together with other gameObject?
Okay, so: I want to make gameobject within another gameobjects. There are two pipes with a hole between them, where I want to spawn a coin on every forth pair of pipes. I already managed to make the spawning but I cannot make them move together. Here is my code in CS. Basically I want to make something like Flappy Bird, I am pretty new to unity and cs, I am just gaining experience. I am pasting my whole code because I do not know where the problem might be.
class PoolObject {
public Transform transform;
public bool inUse;
public PoolObject(Transform t) { transform = t; }
public void Use() { inUse = true; }
public void Dispose() { inUse = false; }
}
[System.Serializable]
public struct YSpawnRange {
public float min;
public float max;
}
public GameObject Prefab;
public GameObject Coin;
public int poolSize;
public int coinCounter;
public float shiftSpeed;
public float spawnRate;
public YSpawnRange ySpawnRange;
public Vector3 defaultSpawnPos;
public bool spawnImmediate;
public Vector3 immediateSpawnPos;
public Vector2 targetAspectRatio;
float spawnTimer;
PoolObject[] poolObjects;
float targetAspect;
GameManager game;
void Awake() {
Configure();
}
void Start() {
game = GameManager.Instance;
}
void OnEnable() {
GameManager.OnGameOverConfirmed += OnGameOverConfirmed;
}
void OnDisable() {
GameManager.OnGameOverConfirmed -= OnGameOverConfirmed;
}
void OnGameOverConfirmed() {
for (int i = 0; i < poolObjects.Length; i++) {
poolObjects[i].Dispose();
poolObjects[i].transform.position = Vector3.one * 1000;
}
// Configure();
if (spawnImmediate) {
SpawnImmediate();
}
}
void Update() {
if (game.GameOver) return;
Shift();
spawnTimer += Time.deltaTime;
if (spawnTimer > spawnRate) {
Spawn();
spawnTimer = 0;
}
}
void Configure() {
//spawning pool objects
targetAspect = targetAspectRatio.x / targetAspectRatio.y;
poolObjects = new PoolObject[poolSize];
for (int i = 0; i < poolObjects.Length; i++) {
GameObject go = Instantiate(Prefab) as GameObject;
Transform t = go.transform;
t.SetParent(transform);
t.position = Vector3.one * 1000;
poolObjects[i] = new PoolObject(t);
}
if (spawnImmediate) {
SpawnImmediate();
}
}
void Spawn() {
//moving pool objects into place
Transform t = GetPoolObject();
if (t == null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
pos.x = (defaultSpawnPos.x * Camera.main.aspect) / targetAspect;
if (coinCounter == 4){
GameObject coin = Instantiate(Coin) as GameObject;
Transform coinPos = coin.transform;
Vector3 pos2 = Vector3.zero;
pos2 = Vector3.Lerp(pos, pos, Random.value);
coinPos.position = pos2;
coin.transform.position = pos2;
coinPos.transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
coinCounter = 0;
}
if (coinCounter < 4){
coinCounter++;
}
t.position = pos;
}
void SpawnImmediate() {
Transform t = GetPoolObject();
// if (t==null) return;
// Vector3 pos = Vector3.zero;
// pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
// t.position = pos;
if (t == null) return;
Vector3 pos = Vector3.zero;
pos.y = Random.Range(ySpawnRange.min, ySpawnRange.max);
pos.x = (immediateSpawnPos.x * Camera.main.aspect) / targetAspect;
t.position = pos;
Spawn();
}
void Shift() {
//loop through pool objects
//moving them
//discarding them as they go off screen
for (int i = 0; i < poolObjects.Length; i++) {
poolObjects[i].transform.position += -Vector3.right * shiftSpeed * Time.deltaTime;
CheckDisposeObject(poolObjects[i]);
}
}
void CheckDisposeObject(PoolObject poolObject) {
//place objects off screen
if (poolObject.transform.position.x < (-defaultSpawnPos.x * Camera.main.aspect) / targetAspect) {
poolObject.Dispose();
poolObject.transform.position = Vector3.one * 1000;
}
}
Transform GetPoolObject() {
//retrieving first available pool object
for (int i = 0; i < poolObjects.Length; i++) {
if (!poolObjects[i].inUse) {
poolObjects[i].Use();
return poolObjects[i].transform;
}
}
return null;
}
Your answer
Follow this Question
Related Questions
Is it possible to use gyroscope to produce a parallax tilt effect in Unity 2D? 0 Answers
Convert Keyboard controlls to UI Buttons 1 Answer
Performance question: Is it better to Instantiate particle systems OR use same particle system? 1 Answer
Input.Touches Mouse position Shooting Buttons 1 Answer
How can I change 'Order in Layer' 1 Answer