Dans ce script je serialise le nombre de trajet en incluant la modification par trajet, la vitesse, si je créée dès le départ des véhicules ou pas et les temps aléatoire de création des véhicules .
Par contre le pourcentage de type de véhicule est commun à tous.
Il y a très longtemps que je l'ai fait et je ne me rappelle plus comment faire pour que la partie de pourcentage de création soit serialisé individuellement pour chaque trajet et non plus communs à tous.
En clair pour le trajet Element 0, je ne voudrais mettre que 100% de PUtilitaire alors que pour Element 1 , je voudrai x% de PSemi, y % de Pbus ....
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
using System;
// donne le% de véhicules
[Serializable]
public struct Trajets {
public GameObject trajet;
public float vitesse;
public float mEntrePoint;
public DepartVl[] departVl;
}
[Serializable]
public struct DepartVl {
public int positionDepart;
public float vitesseDepart;
}
public enum InterieurVl {
None,
Oui,
Non
}
public class DebutRoute : MonoBehaviour {
public Trajets[] trajets;
private int choixTrajet;
// pour multiplier le temps d'apparition des vl la nuit
public float multiplicateurNuit = 1.0f;
// temps apparition entre chaque voiture choisi au hasard
public float[] tempsApparition; // dans inspector donner le nombre de temsp puis les renseigner
//choisis un temps aléatoire
private int choixTemps;
private float temps;
public float pCar;
public float pUtilitaire;
public float pSemi;
public float pBus;
public float SpSemiLong;
public float pMoto;
private int choixPourcentage;
private bool finStart;
private Light jourNuit;
void Start () {
choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
choixPourcentage = UnityEngine.Random.Range(1,101);
if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto > 100)
Debug.LogError("il y a plus de 100%");
if (pCar + pUtilitaire + pSemi + pBus + SpSemiLong + pMoto < 100)
Debug.LogError("il y a moin de 100%");
for (int i = 0; i < trajets.Length; i++) {
CreationVlDepart (i);
}
// 1 ° gamobject car seul dans la hierarchy.on va chercher sky Dome
// je prends son enfant Light
// dans Inspector on prend Light
jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();
finStart = true;
}
// Update is called once per frame
void Update () {
if (finStart && Time.timeScale != 0) {
temps += Time.fixedDeltaTime;
// on multiplie le temps d'apparition si light < à 0.15
float tempsTempoApparition = 0.0f;
if ( jourNuit.intensity < 0.15f) {
tempsTempoApparition = tempsApparition[choixTemps] * multiplicateurNuit;
}
else {
tempsTempoApparition = tempsApparition[choixTemps];
}
//automatiquement
if (temps >= tempsTempoApparition) {
CreationVl(trajets[choixTrajet].trajet, trajets[choixTrajet].mEntrePoint, trajets[choixTrajet].vitesse, 0);
choixTemps = UnityEngine.Random.Range(0,tempsApparition.Length);
choixTrajet = UnityEngine.Random.Range(0,trajets.Length);
choixPourcentage = UnityEngine.Random.Range(1,101);
temps = 0;
}
}
}
void CreationVlDepart (int countTrajet) {
if (trajets[countTrajet].departVl.Length != 0) {
for (int i = 0; i < trajets[countTrajet].departVl.Length; i++) {
CreationVl (trajets[countTrajet].trajet, trajets[countTrajet].mEntrePoint, trajets[countTrajet].departVl[i].vitesseDepart, trajets[countTrajet].departVl[i].positionDepart);
}
}
}
public void CreationVl (GameObject trajet, float metre, float vitesseFraction, int position) {
string nom = "";
int choixVl = 0;
InterieurVl interieur = InterieurVl.None;
//voitures
if (choixPourcentage < pCar) {
choixVl = UnityEngine.Random.Range(1,19); //19car il y a 18 voitures + 1
nom = "Voitures/Car_";
interieur = InterieurVl.Oui;
//utilitaires
} else if (choixPourcentage < pCar + pUtilitaire) {
choixVl = UnityEngine.Random.Range(1,14);//14 car il y a 13 utilitaires+ 1
nom = "Utilitaires/utilitaire_";
interieur = InterieurVl.Oui;
//semi
} else if (choixPourcentage < pCar + pUtilitaire + pSemi) {
choixVl = UnityEngine.Random.Range(1,9);//car 8 camions ne pas prendre les porte char et le double remoque
nom = "Poids_Lourds/semi_";
interieur = InterieurVl.Oui;
//bus
} else if (choixPourcentage < pCar + pUtilitaire + pSemi + pBus) {
choixVl = UnityEngine.Random.Range(1,4);//car 3 bus +1
nom = "BUS/bus_";
interieur = InterieurVl.Oui;
//semi long 2 remorque et porte char
} else if (choixPourcentage < pCar + pUtilitaire + pSemi+ pBus + SpSemiLong) {
choixVl = UnityEngine.Random.Range(1,4);//car 2 porte chars et le double remoque
nom = "Super_Poids_Lourds_Longs/semiLong_";
interieur = InterieurVl.Oui;
//moto
} else if (choixPourcentage <= pCar + pUtilitaire + pSemi+ pBus + SpSemiLong + pMoto) {
choixVl = UnityEngine.Random.Range(1,9);//9
nom = "Motos/moto_";
interieur = InterieurVl.Non;
}
GameObject instance = Instantiate(Resources.Load(""+nom+choixVl,typeof(GameObject))) as GameObject;
//modif du non
instance.name += trajet.name;
//change la postion de l'object dans le scéne
//donc transform = à l'obejct qui contient le script DebutRoute
instance.transform.SetParent(transform);
if (GetComponent<TrajetVl>() != null) {
//set le parcour sur l'object
instance.GetComponent<TrajetVl>().target_Path = trajet;
//modif la vitesse de l'object
instance.GetComponent<TrajetVl>().kmh = vitesseFraction;
instance.GetComponent<TrajetVl>().kmhActuel = vitesseFraction;
instance.GetComponent<TrajetVl>().minTab = position;
instance.GetComponent<TrajetVl>().mEntrePoint = metre;
instance.GetComponent<TrajetVl>().interieur = interieur;
instance.GetComponent<TrajetVl>().DemrageVl();
} else {
//set le parcour sur l'object
instance.GetComponentInChildren<TrajetVl>().target_Path = trajet;
//modif la vitesse de l'object
instance.GetComponentInChildren<TrajetVl>().kmh = vitesseFraction;
instance.GetComponentInChildren<TrajetVl>().kmhActuel = vitesseFraction;
instance.GetComponentInChildren<TrajetVl>().minTab = position;
instance.GetComponentInChildren<TrajetVl>().mEntrePoint = metre;
instance.GetComponentInChildren<TrajetVl>().interieur = interieur;
instance.GetComponentInChildren<TrajetVl>().DemrageVl();
}
}
}