Je m'en remet à vous encore un fois ! Au niveau du Timer cette fois-ci ! Je suis entrain de créer un petit quelque chose assez sympa à mon gouts.
Le but du jeux et de faire en sorte que le joueur survit à un temps chronométré, il doit esquiver des objets lui tombant dessus. Ça à l'air de fonctionne je dois dire. Au cours du mini, tout en esquivant les objets, il y a d'autre items, qui rapporte des points, se qu'on peut appeler des gem's. Enfin bref...
Je voudrais faire en sorte qu'une fois le compte à rebours arrivé à 0, le joueurs à gagné, il peux passé à la suite. Donc, pour allez à la salle suivant, il devra allez manuellement, la porte survivra une fois le compte à rebours à 0. Donc les objets continuera de tomber, sauf les objets dites (Gem).
Je sais pas comment faire ça... Je pense que j'ai mélange pas mal de tuto sortie tout droit d'internet.
Je voudrais faire en sorte que les Gem's apparaissent entre de secondes par exemple aléatoirement entre 2 et 7 secondes, pour donner plus de dynamisme.
Je vais vous donner les codes que j'ai pour le moment, les codes dont je pense qui faudrait modifier.
ItemsSpawn => Affiches les item aléatoirement, je voudrais qu'ici on puissent grâce au Timer faire apparaitre des gems aléatoirement, par exemple faire en sorte que entre 1 et 5 secondes, une gems apparaisse. Pour pas que le joueur s'y attend.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemsSpawn : MonoBehaviour
{
#region Variable
//Public
//Private
[SerializeField] private float minX = 0.0f;
[SerializeField] private float maxX = 0.0f;
[SerializeField] private int minItemsToSpawn = 1;
[SerializeField] private int maxItemsToSpawn = 6;
[SerializeField] private GameObject[] items; // potential array of hazards
[SerializeField] private float timeBetweenSpawns = 0.0f;
private bool canSpawn = false;
private int qteItemsToSpawn = 0; // QTE => Quantité, Quantité de projectile à apparaitre.
private int itemToSpawn = 0;
private int itemSpawnCap = 8;
#endregion
#region UnityFunction
// Start is called before the first frame update
void Start()
{
canSpawn = true; //Temp start
}
// Update is called once per frame
void Update()
{
if (canSpawn == true)
{
StartCoroutine("GenerateItems");
}
}
private IEnumerator GenerateItems()
{
canSpawn = false;
timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing Values
qteItemsToSpawn = Random.Range(minItemsToSpawn, maxItemsToSpawn); //Testing Values
for (int i = 0; i < qteItemsToSpawn; i++)
{
Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), Random.Range(8.5f, 12.0f), 0.0f); // Generate a spawnPosition the Hazard
Instantiate(items[itemToSpawn], spawnPos, Quaternion.identity); // Spawn the Hazard
}
yield return new WaitForSeconds(timeBetweenSpawns);
canSpawn = true;
}
#endregion
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimedSpawn : MonoBehaviour
{
#region Variable
public GameObject spawnee;
public bool stopSpawning = false;
public float spawnTime;
public float spawnDelay;
#endregion
#region UnityFunction
// Start is called before the first frame update
void Start()
{
InvokeRepeating("SpawnObject", spawnTime, spawnDelay);
}
public void SpawnObject()
{
Instantiate(spawnee, transform.position, transform.rotation);
if (stopSpawning)
{
CancelInvoke("SpawnObject");
}
}
// Update is called once per frame
void Update()
{
}
#endregion
}
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Countdownw : MonoBehaviour
{
#region Variable
[SerializeField] public float time = 0f;
#endregion
#region UnityFunction
// Start is called before the first frame update
void Start()
{
StartCoroutine (timer ());
time += 1;
}
IEnumerator timer()
{
while (time > 0)
{
time--;
yield return new WaitForSeconds (1f);
GetComponent<Text> ().text = string.Format ("{0:0}:{1:00}", Mathf.Floor (time / 60), time % 60);
}
if(time == 0)
{
Debug.Log("Boom");
}
}
// Update is called once per frame
void Update()
{
}
#endregion
}
Grâce a la variable minProjectilesToSpawn & miXProjectilesToSpawn, je peux afficher un nombre aléatoire à apparaitre à chaque fois. Par exemple 1 & 6 il y aura entre 1 et 6 projectiles qui apparaitrons à chaque fois que sa Spawn.
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectilesSpawn : MonoBehaviour
{
#region Variable
//Public
//Private
[SerializeField] private float minX = 0.0f;
[SerializeField] private float maxX = 0.0f;
[SerializeField] private int minProjectilesToSpawn = 1;
[SerializeField] private int maxProjectilesToSpawn = 6;
[SerializeField] private GameObject[] Projectiles; // potential array of hazards
[SerializeField] private float timeBetweenSpawns = 0.0f;
private bool canSpawn = false;
private int qteProjectilesToSpawn = 0; // QTE => Quantité, Quantité de projectile à apparaitre.
private int projectileToSpawn = 0;
private int projectileSpawnCap = 8;
#endregion
#region UnityFunction
// Start is called before the first frame update
void Start()
{
canSpawn = true; //Temp start
}
// Update is called once per frame
void Update()
{
if (canSpawn == true)
{
StartCoroutine("GenerateProjectiles");
}
}
private IEnumerator GenerateProjectiles()
{
canSpawn = false;
timeBetweenSpawns = Random.Range(0.5f, 2.0f); //Testing Values
qteProjectilesToSpawn = Random.Range(minProjectilesToSpawn, maxProjectilesToSpawn); //Testing Values
for (int i = 0; i < qteProjectilesToSpawn; i++)
{
Vector3 spawnPos = new Vector3(Random.Range(minX, maxX), Random.Range(8.5f, 12.0f), 0.0f); // Generate a spawnPosition the Hazard
Instantiate(Projectiles[projectileToSpawn], spawnPos, Quaternion.identity); // Spawn the Hazard
}
yield return new WaitForSeconds(timeBetweenSpawns);
canSpawn = true;
}
#endregion
}
Code : Tout sélectionner
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ProjectilesCollision : MonoBehaviour
{
#region Variable
//Public
//Private
[SerializeField] private ParticleSystem projectilesParticlesToon;
#endregion
#region UnityFunction
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Platform")
{
Destroy(Instantiate(projectilesParticlesToon.gameObject, transform.position, projectilesParticlesToon.transform.rotation), projectilesParticlesToon.startLifetime);
this.gameObject.SetActive(false);
}
if (collision.gameObject.tag == "Player")
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
#endregion
}