[MY-AL] Bool aléatoire à chaque création de clone ?

Pour les scripts écrits en C#
Règles du forum
Merci de respecter la NOMENCLATURE suivante pour vos TITRES de messages :

Commencez par le niveau de vos scripts
DB = Débutant
MY = Moyen
CF = Confirmé

Puis le domaine d'application
-RS = Réseau
-AL = Algorithmie

Exemple :

[DB-RS] Mouvement perso multijoueur
Répondre
Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

[MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 03 Mars 2022 19:09

Bonjour
Avec mon script TrajetVl, je créée des véhicules avec le bool Coupe détecteur à false par défaut.
Ce détecteur me permet de faire des clignotants ( actif ou inactif) avec des cubes avec le script ci dessous
dans lequel je renseigne le nom du trajet , la vitesse et aussi quel coté du clignotant doit s'activer
code sur le cube pour activer les clignotants

Code : Tout sélectionner

using UnityEngine;
using System.Collections;

public enum EnumClignotants {Droites, Gauches}

public class GestionClignotants : MonoBehaviour {

	public GameObject trajet;
	public EnumClignotants enumClignotants;
	public float vitesse;

}
Caractéristiques des véhicules créés sans le bool Coupe Détecteur actif ( fonctionnement normal)
ScreenShot134.jpg
ScreenShot134.jpg (55.73 Kio) Consulté 3448 fois
Voici donc le script où le bool Coupe détecteur n'est pas actif au départ pour tous les véhicules créés ce qui est normal

Code : Tout sélectionner

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/* 
 *   A TOUTES LES CREATIONS DE VEHICULE  LE DETECTEUR EST NON COCHE 
 */
public class TrajetVl : MonoBehaviour {
	public GameObject target_Path;
	public float kmh; // vitesse voulue
	public float kmhActuel; // vitesse reelle
	public float mEntrePoint = 5.0f;
	private float time;
	public float fraction;
	public Transform[] tabtList = new Transform[11];
	public int minTab = 0;
	public Transform detecteur;
	public LayerMask layerPanneau;
	public Collider[] toucheDetecteur;
	public bool coupeDetecteur;
	private Light jourNuit;
	private GameObject eclairage;
	private GameObject eclairageFreins;
	public GameObject clignotantsDroits;
	public GameObject clignotantsGauche;
	private IEnumerator coroutineClignotants;
	
	public InterieurVl interieur;
	
	public void DemrageVl () {
		tabtList = new Transform[11];
		TabDynamique ();
		if (transform.FindChild("Detecteur"))
			detecteur = transform.FindChild("Detecteur").transform;
		
		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();
		
		//cherche dans ces enfants un object avec le nom Eclairage
		//si existe alors on ajoute l'object portant le nom "Eclairage" dans la variable eclairage
		if (this.transform.FindChild ("Eclairage")) {
			eclairage = this.transform.FindChild ("Eclairage").gameObject;
		}
		
		if (this.transform.FindChild ("Eclairage_Freins")) {
			eclairageFreins = this.transform.FindChild ("Eclairage_Freins").gameObject;
			eclairageFreins.SetActive (false);
		}
		
		if (this.transform.FindChild ("Eclairage_Clignotants_droits")) {
			clignotantsDroits = this.transform.FindChild ("Eclairage_Clignotants_droits").gameObject;
			clignotantsDroits.SetActive (false);
		}
		
		if (this.transform.FindChild ("Eclairage_Clignotants_gauche")) {
			clignotantsGauche = this.transform.FindChild ("Eclairage_Clignotants_gauche").gameObject;
			clignotantsGauche.SetActive (false);
		}
		
		StartCoroutine("WalkThePath");
	}
	
	public void TabDynamique () {
		fraction = 0.1f;
		int count = 0;
		
		for (int i = 0; i < 11; i++) {
			if (minTab+i < target_Path.transform.childCount) {
				tabtList[i] = target_Path.transform.GetChild(minTab+i);
				count++;
			}
		}
		
		if (minTab+8 < target_Path.transform.childCount)
			minTab += 8;
		else
			minTab += count;
	}
	
	public void StopCoroutine () {
		StopCoroutine("WalkThePath");
	}
	
	IEnumerator WalkThePath () {
		while(true){
			time = (tabtList.Length * mEntrePoint)/(kmhActuel / 3.6f);
			transform.position = iTween.PointOnPath (tabtList, fraction);
			fraction += Time.deltaTime / time;
			if (fraction > 1.0f)
				fraction = 0.0f;
			
			/* modif pour vehicule ou personnage*/
			
			
			if (gameObject.tag == "VEHICULE")
			{
				transform.LookAt(iTween.PointOnPath(tabtList, fraction + 0.05f)); // fraction + 0.1f
			}
			else 
			{
				Vector3 direction =iTween.PointOnPath(tabtList, fraction + 0.05f);//fraction + 0.1f
				direction.y = this.transform.position.y;
				transform.LookAt(direction);
			}
			
			
			
			
			
			if (kmhActuel < kmh){
				kmhActuel += 0.7f;// acceleration jusqu'à la vitesse kmh 0.7f
			} else if (kmhActuel > kmh && kmh != 0) {
				kmhActuel -= 0.9f;// decceleration douce 0.9
			} else if (kmh == 0) {
				kmhActuel = 0;// on stoppe brutalement le VL
			}
			
			if (fraction >= 0.9f) //0.9f
				TabDynamique();
			
			GestionDetecteur ();
			GestionLumiere ();
			GestionFreins ();
			
			yield return null;
		}
	}
	
	void GestionDetecteur () {
		if (detecteur != null) {
			if (gameObject.tag == "VEHICULE")
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.75f, layerPanneau);
			else
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.5f, layerPanneau);
			
			if (toucheDetecteur.Length != 0) {
				foreach (var other in toucheDetecteur) {
					if (other.tag == "CoupeDetecteur") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 50.0f;// pour degager les croisements
					}
					
					if (other.tag == "CoupeDetecteurAutre") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 4.0f;
					}
					
					if (coupeDetecteur == false) {
						if (other.tag == "ModifVitesse") {
							kmh = other.GetComponent<Panneau>().vitesse;
							
							
						} else if (other.tag == "EntreeInstersection") {
							kmh = other.GetComponent<Priorite>().vitesse;
						} else if (other.tag == "VEHICULE") {
							
							if (kmh > other.GetComponent<TrajetVl>().kmh)
								kmh = other.GetComponent<TrajetVl>().kmh;
						} else if (other.tag == "ClignotantsOn") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							if (other.GetComponent<GestionClignotants>().trajet == target_Path) {
								ClignotantsOff ();
								if (other.GetComponent<GestionClignotants>().enumClignotants == EnumClignotants.Droites) {
									coroutineClignotants = ClignotantsDroits();
									StartCoroutine (coroutineClignotants);
								} else {
									coroutineClignotants = ClignotantsGauche();
									StartCoroutine (coroutineClignotants);
								}
							}
						} else if (other.tag == "ClignotantsOff") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							ClignotantsOff ();
						}
					}
				}
			}
			
			if (kmhActuel == 0 && toucheDetecteur.Length == 0)
				kmhActuel = 30.0f;
			
		}
	}
	
	void GestionLumiere () {
		if (eclairage != null) {
			if (jourNuit.intensity < 0.15f) {
				if (eclairage.activeInHierarchy == false)
					eclairage.SetActive (true);
			}
			else {
				if (eclairage.activeInHierarchy == true)
					eclairage.SetActive (false);
			}
		}
	}
	
	void GestionFreins () {
		if (eclairageFreins != null){
			if (kmhActuel > kmh+3 || kmhActuel == 0) {
				if (eclairageFreins.activeInHierarchy == false)
					eclairageFreins.SetActive (true);
			} else {
				if (eclairageFreins.activeInHierarchy == true)
					eclairageFreins.SetActive (false);
			}
		}
	}
	
	IEnumerator ClignotantsDroits () {
		if (clignotantsDroits != null) {
			while (true) {
				clignotantsDroits.SetActive (true);
				yield return new WaitForSeconds (0.3f);
				clignotantsDroits.SetActive (false);
				yield return new WaitForSeconds (0.3f);
			}
		}
	}
	
	IEnumerator ClignotantsGauche () {
		if (clignotantsGauche != null) {
			while (true) {
				clignotantsGauche.SetActive (true);
				yield return new WaitForSeconds (0.3f);
				clignotantsGauche.SetActive (false);
				yield return new WaitForSeconds (0.3f);
			}
		}
	}
	
	void ClignotantsOff () {
		if (coroutineClignotants != null) {
			StopCoroutine (coroutineClignotants);
			
			if (clignotantsDroits != null)
				clignotantsDroits.SetActive (false);
			if (clignotantsGauche != null)
				clignotantsGauche.SetActive (false);
			
			coroutineClignotants = null;
		}
	}
	
}
J'ai fait une autre maquette en modifiant ce script mais je n'ai pas touché à la partie portant sur les clignotants ni le detecteur
Mais maintenant lors de la création des véhicules , aléatoirement le bool Coupe detecteur s' active ?!
Du coup les clignotants ne peuvent plus être activés , ainsi que d'autres fonctions.
ScreenShot135.jpg
ScreenShot135.jpg (59.3 Kio) Consulté 3448 fois
voici le code posant pb. Je ne vois pas d'où cela vient cette activation aléatoire

Code : Tout sélectionner

using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
// la fonction de trajet aérien est bloquée
/* 
 * 
 *    ????????? VALIDATION ALEATOIRE DE COUPE DETECTEUR ??????????
 * 
 * 
 * 
 */
public class TrajetVl : MonoBehaviour {
	public GameObject target_Path;
	public float kmh; // vitesse voulue
	public float kmhActuel; // vitesse reelle
	public float mEntrePoint = 5.0f;
	private float time;
	public float fraction;
	public Transform[] tabtList = new Transform[11];
	public int minTab = 0;
	public Transform detecteur;
	public LayerMask layerPanneau;
	public Collider[] toucheDetecteur;
	public bool coupeDetecteur;
	private Light jourNuit;                    //private
	private GameObject eclairage;             //private
	private bool dansUnTunnel = false;       // pour allumage des feux dans un tunnel, le tunnel est tagué en TUNNEL et a la forme 3D du tunnel réel

	private GameObject eclairageFreins;
	public GameObject clignotantsDroits;
	public GameObject clignotantsGauche;
	private IEnumerator coroutineClignotants;
	
	public InterieurVl interieur;
	
	public void DemrageVl () {
		tabtList = new Transform[11];
		TabDynamique ();
		if (transform.FindChild("Detecteur"))
			detecteur = transform.FindChild("Detecteur").transform;
		
		jourNuit = GameObject.Find("Sky Dome").transform.FindChild("Light").GetComponent<Light>();
		
		//cherche dans ces enfants un object avec le nom Eclairage
		//si existe alors on ajoute l'object portant le nom "Eclairage" dans la variable eclairage
		if (this.transform.FindChild ("Eclairage")) {
			eclairage = this.transform.FindChild ("Eclairage").gameObject;
		}
		
		if (this.transform.FindChild ("Eclairage_Freins")) {
			eclairageFreins = this.transform.FindChild ("Eclairage_Freins").gameObject;
			eclairageFreins.SetActive (false);
		}
		
		if (this.transform.FindChild ("Eclairage_Clignotants_droits")) {
			clignotantsDroits = this.transform.FindChild ("Eclairage_Clignotants_droits").gameObject;
			clignotantsDroits.SetActive (false);
		}
		
		if (this.transform.FindChild ("Eclairage_Clignotants_gauche")) {
			clignotantsGauche = this.transform.FindChild ("Eclairage_Clignotants_gauche").gameObject;
			clignotantsGauche.SetActive (false);
		}
		
		StartCoroutine("WalkThePath");
	}
	
	public void TabDynamique () {
		fraction = 0.1f;
		int count = 0;
		
		for (int i = 0; i < 11; i++) {
			if (minTab+i < target_Path.transform.childCount) {
				tabtList[i] = target_Path.transform.GetChild(minTab+i);
				count++;
			}
		}
		
		if (minTab+8 < target_Path.transform.childCount)
			minTab += 8;
		else
			minTab += count;
	}
	
	public void StopCoroutine () {
		StopCoroutine("WalkThePath");
	}


	/*
    // version du createur DOTWEENN
    void LateUpdate () {
        //nombre de point * distance entre point / km/h / 3.6
        time = (tabt.Length * mEntrePoint)/(kmh / 3.6f);
        if (start) {			
            transform.position = iTween.PointOnPath(tabt, fraction);
			
            fraction += Time.deltaTime / time;
            if (fraction > 1.0f)
                fraction = 0.0f;
			
            transform.LookAt(iTween.PointOnPath(tabt, fraction + 0.001f));
         }
    }*/

	IEnumerator WalkThePath () {
		while(true){
			time = ((tabtList.Length * mEntrePoint)/(kmhActuel / 3.6f)*0.88f);//   original (tabtList.Length * mEntrePoint)/(kmhActuel / 3.6f)   
			/*
			scene 2x2
			362 x 5m = 1 810m 
				130 kmh/h fait 36.1111 mn/s        donc   1 810m à 130km/h  fait  50 sec / 55 sec réelles donc x0.91 pour avoir 50 sec
				110 kmh/h fait 30.5555 mn/s        donc   1 810m à 110km/h  fait  59 sec / 64 sec réelles donc x0.91 pour avoir 59 sec


             scene bi-di
             305 x 5m = 1 525m 
				90 kmh/h fait 25     mn/s        donc   1 525m à 90km/h  fait  61 sec /  66 sec réelles donc x0.91 pour avoir 60 sec
				50 kmh/h fait 13.888 mn/s        donc   1 525m à 50km/h  fait  110 sec/ 117 sec réelles donc x0.91 pour avoir 107 sec

			 */



			transform.position = iTween.PointOnPath (tabtList, fraction);
			fraction += Time.deltaTime / time;
			if (fraction > 1.0f)
				fraction = 0.0f;
			
			/* modif pour vehicule ou personnage*/
			

			if (gameObject.tag == "VELO" || gameObject.tag == "VEHICULE" || gameObject.tag == "BUS"/*Et tous les autres comme MOTO, CAMION, etc*/) 
			{
				transform.LookAt(iTween.PointOnPath(tabtList, fraction + 0.08f)); //0.05F  plus c'est petit est plus de tremblote pour s'orienter sur le point à 0.005m
			}
		

			else //c'est les pietons, qui restent verticaux, ou la camera aérienne qui n'a pas besoin de suivre la pente du terrain 
			{
				Vector3 direction =iTween.PointOnPath(tabtList, fraction + 0.08f); //0.05F
				direction.y = this.transform.position.y;
				transform.LookAt(direction);
			}

			
			
			if (kmhActuel < kmh){
				kmhActuel += 0.7f;// acceleration jusqu'à la vitesse kmh 0.7f
			} else if (kmhActuel > kmh && kmh != 0) {
				kmhActuel -= 0.9f;// decceleration douce 0.9
			} else if (kmh == 0) {
				kmhActuel = 0;// on stoppe brutalement le VL
			}
			
			if (fraction >= 0.9f) //0.9f
				TabDynamique();
			
			GestionDetecteur ();
			GestionLumiere ();
			GestionFreins ();
			
			yield return null;
		}
	}
	
	void GestionDetecteur () {
		if (detecteur != null) {
			if (gameObject.tag == "VEHICULE")
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.75f, layerPanneau);
			else
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.5f, layerPanneau);
			
			if (toucheDetecteur.Length != 0) {
				foreach (Collider other in toucheDetecteur) {
					if (other.tag == "CoupeDetecteur") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 30.0f;// pour degager les croisements  à 30 km/h 
						                  // tant que la sphere du détecteur est dans l'objet sa vitesse est de 30 km/h
						                  // apres le VL reprend sa vitesse normale
					}
					
					if (other.tag == "CoupeDetecteurAutre") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 4.0f; 
					}

/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant
					
					if (other.tag == "VEHICULE") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
//////////////////////////





/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant

					if (other.tag == "VELO") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
//////////////////////////
					  
					if (coupeDetecteur == false) {
						if (other.tag == "ModifVitesse") {
							kmh = other.GetComponent<Panneau>().vitesse;
							
							
						} else if (other.tag == "EntreeInstersection") {
							kmh = GetPrioriteVitesse(other);
						} else if (other.tag == "VEHICULE") {
							
							if (kmh > other.GetComponent<TrajetVl>().kmh)
								kmh = other.GetComponent<TrajetVl>().kmh;
						} else if (other.tag == "ClignotantsOn") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							if (other.GetComponent<GestionClignotants>().trajet == target_Path) {
								ClignotantsOff ();
								if (other.GetComponent<GestionClignotants>().enumClignotants == EnumClignotants.Droites) {
									coroutineClignotants = ClignotantsDroits();
									StartCoroutine (coroutineClignotants);
								} else {
									coroutineClignotants = ClignotantsGauche();
									StartCoroutine (coroutineClignotants);
								}
							}
						} else if (other.tag == "ClignotantsOff") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							ClignotantsOff ();
						}
					}
				}
			}
			
			if (kmhActuel == 0 && toucheDetecteur.Length == 0)
				kmhActuel = 30.0f;
			
		}
	}
//------------------------------------------------------------------------------------
//                allumage et extinction des feux dans un tunnel

		void OnTriggerEnter(Collider other) //ENTREE DU TUNNEL 
			//le collider MAYA 3D en forme de tunnel doit etre tagué TUNNEL et que ce script est attaché a un objet tagué VEHICULE ou VELO
		{                               
			if (other.tag == "TUNNEL" && (gameObject.tag == "VEHICULE" || gameObject.tag == "VELO")) 
			{
				dansUnTunnel = true;
			}
		}
		
		void OnTriggerExit(Collider other) //SORTIE DU TUNNEL (UN SEUL COLLIDER ENTIER MAYA 3D en forme de tunnel)
		{
			if (other.tag == "TUNNEL" && (gameObject.tag == "VEHICULE" || gameObject.tag == "VELO"))
			{
				dansUnTunnel = false;
			}
		}
		

// ------------------------------
	void GestionLumiere () {
		if (eclairage != null)
		{
			// SI TU ES DANS UN TUNNEL TU ALLUMES LES FEUX QUEL QUE SOIT LE JOUR/NUIT
			if (dansUnTunnel) {
				eclairage.SetActive (true);
			}
			// SINON C'EST LE JOUR/NUIT QUI S'EN OCCUPE
			else {
				if (jourNuit.intensity < 0.15f) {
					if (eclairage.activeInHierarchy == false)
						eclairage.SetActive (true);
				}
				else {
					if (eclairage.activeInHierarchy == true)
						eclairage.SetActive (false);
				}
			}
		}
	}
	
	void GestionFreins () {
		if (eclairageFreins != null){
			if (kmhActuel > kmh+3 || kmhActuel == 0) {
				if (eclairageFreins.activeInHierarchy == false)
					eclairageFreins.SetActive (true);
			} else {
				if (eclairageFreins.activeInHierarchy == true)
					eclairageFreins.SetActive (false);
			}
		}
	}
	
	IEnumerator ClignotantsDroits () {
		if (clignotantsDroits != null) {
			while (true) {
				clignotantsDroits.SetActive (true);
				yield return new WaitForSeconds (0.3f);
				clignotantsDroits.SetActive (false);
				yield return new WaitForSeconds (0.3f);
			}
		}
	}
	
	IEnumerator ClignotantsGauche () {
		if (clignotantsGauche != null) {
			while (true) {
				clignotantsGauche.SetActive (true);
				yield return new WaitForSeconds (0.3f);
				clignotantsGauche.SetActive (false);
				yield return new WaitForSeconds (0.3f);
			}
		}
	}
	
	void ClignotantsOff () {
		if (coroutineClignotants != null) {
			StopCoroutine (coroutineClignotants);
			
			if (clignotantsDroits != null)
				clignotantsDroits.SetActive (false);
			if (clignotantsGauche != null)
				clignotantsGauche.SetActive (false);
			
			coroutineClignotants = null;
		}
	}

	float GetPrioriteVitesse(Collider other){
		Priorite priorite = other.GetComponent<Priorite> ();
		if (priorite != null) {
			return priorite.vitesse;
		} else {
			PrioriteOllioules prioriteOllioules = other.GetComponent<PrioriteOllioules> ();
			return prioriteOllioules.vitesse;
		}
	}
	
}
Merci

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 03 Mars 2022 21:06

J'ai ajouté le bool en false mais le pb est toujours là

Code : Tout sélectionner

public bool coupeDetecteur = false;

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6212
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par boubouk50 » 04 Mars 2022 10:23

Il n'y a rien de magique.
S'il passe à True, c'est qu'il remplit une condition pour être passé à True.
Quelque part dans ton code, tu as une condition remplie qui l'active.

Code : Tout sélectionner

if (toucheDetecteur.Length != 0) {
				foreach (Collider other in toucheDetecteur) {
					if (other.tag == "CoupeDetecteur") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 30.0f;// pour degager les croisements  à 30 km/h 
						                  // tant que la sphere du détecteur est dans l'objet sa vitesse est de 30 km/h
						                  // apres le VL reprend sa vitesse normale
					}
					
					if (other.tag == "CoupeDetecteurAutre") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 4.0f; 
					}
										
					if (other.tag == "VEHICULE") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
					
					if (other.tag == "VELO") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
Une de ces conditions est remplie et le détecteur récupéré est à true.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 04 Mars 2022 11:08

Bonjour
Voici la partie du code d'origine avec les clignotants ok

Code : Tout sélectionner

void GestionDetecteur () {
		if (detecteur != null) {
			if (gameObject.tag == "VEHICULE")
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.75f, layerPanneau);
			else
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.5f, layerPanneau);
			
			if (toucheDetecteur.Length != 0) {
				foreach (var other in toucheDetecteur) {
					if (other.tag == "CoupeDetecteur") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 50.0f;// pour degager les croisements
					}
					
					if (other.tag == "CoupeDetecteurAutre") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 4.0f;
					}
					
					if (coupeDetecteur == false) {
						if (other.tag == "ModifVitesse") {
							kmh = other.GetComponent<Panneau>().vitesse;
							
							
						} else if (other.tag == "EntreeInstersection") {
							kmh = other.GetComponent<Priorite>().vitesse;
						} else if (other.tag == "VEHICULE") {
							
							if (kmh > other.GetComponent<TrajetVl>().kmh)
								kmh = other.GetComponent<TrajetVl>().kmh;
						} else if (other.tag == "ClignotantsOn") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							if (other.GetComponent<GestionClignotants>().trajet == target_Path) {
								ClignotantsOff ();
								if (other.GetComponent<GestionClignotants>().enumClignotants == EnumClignotants.Droites) {
									coroutineClignotants = ClignotantsDroits();
									StartCoroutine (coroutineClignotants);
								} else {
									coroutineClignotants = ClignotantsGauche();
									StartCoroutine (coroutineClignotants);
								}
							}
						} else if (other.tag == "ClignotantsOff") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							ClignotantsOff ();
						}
					}
				}
			}
			
			if (kmhActuel == 0 && toucheDetecteur.Length == 0)
				kmhActuel = 30.0f;
			
		}
	}

voici le code à problème. J'ai juste ajouté les lignes entre les /**********
juste avant la ligne " if (coupeDetecteur == false)"

Code : Tout sélectionner

	void GestionDetecteur () {
		if (detecteur != null) {
			if (gameObject.tag == "VEHICULE")
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.75f, layerPanneau);
			else
				toucheDetecteur = Physics.OverlapSphere(detecteur.position, 0.5f, layerPanneau);
			
			if (toucheDetecteur.Length != 0) {
				foreach (Collider other in toucheDetecteur) {
					if (other.tag == "CoupeDetecteur") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 30.0f;// pour degager les croisements  à 30 km/h 
						                  // tant que la sphere du détecteur est dans l'objet sa vitesse est de 30 km/h
						                  // apres le VL reprend sa vitesse normale
					}
					
					if (other.tag == "CoupeDetecteurAutre") {
						coupeDetecteur = other.GetComponent<CoupeDetecteur>().detecteur;
						kmhActuel = 4.0f; 
					}
//********************************************************
/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant
					
					if (other.tag == "VEHICULE") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant

					if (other.tag == "VELO") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
//*************************************************
					  
					if (coupeDetecteur == false) {
						if (other.tag == "ModifVitesse") {
							kmh = other.GetComponent<Panneau>().vitesse;
							
							
						} else if (other.tag == "EntreeInstersection") {
							kmh = GetPrioriteVitesse(other);
						} else if (other.tag == "VEHICULE") {
							
							if (kmh > other.GetComponent<TrajetVl>().kmh)
								kmh = other.GetComponent<TrajetVl>().kmh;
						} else if (other.tag == "ClignotantsOn") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							if (other.GetComponent<GestionClignotants>().trajet == target_Path) {
								ClignotantsOff ();
								if (other.GetComponent<GestionClignotants>().enumClignotants == EnumClignotants.Droites) {
									coroutineClignotants = ClignotantsDroits();
									StartCoroutine (coroutineClignotants);
								} else {
									coroutineClignotants = ClignotantsGauche();
									StartCoroutine (coroutineClignotants);
								}
							}
						} else if (other.tag == "ClignotantsOff") {
							kmh = other.GetComponent<GestionClignotants>().vitesse;
							ClignotantsOff ();
						}
					}
				}
			}
			
			if (kmhActuel == 0 && toucheDetecteur.Length == 0)
				kmhActuel = 30.0f;
			
		}
	}

Ma modification est juste la suivante à la ligne 184 du code à problème

Code : Tout sélectionner

//********************************************************
/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant
					
					if (other.tag == "VEHICULE") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant

					if (other.tag == "VELO") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
//*************************************************
Pour moi je dis , si le detecteur touche un objet tag VEHICULE ou VELO, mon vl prend la vitesse du vehicule ou du velo qui est devant lui et ne lui roule pas dessus. c'est tout!. Qu'est ce que j'ai raté ?

Merci

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6212
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par boubouk50 » 04 Mars 2022 11:22

Code : Tout sélectionner

coupeDetecteur = other.GetComponent<XXXX>().detecteur;
Ici, coupeDetecteur est affecté donc il peut changer d'état. Donc si tu crées un clone à un endroit où il est en collision avec un autre objet qui a son détecteur à True, il prendra True.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 04 Mars 2022 11:51

J'ai posté en même temps du coup mon post a été supprimé.
Quand un vl est créé il ne touche rien ( en rouge c'est le détecteur)
Car 2 clone est le nom du véhicule
ScreenShot137.jpg
ScreenShot137.jpg (113.93 Kio) Consulté 3389 fois
il est actif
ScreenShot138.jpg
ScreenShot138.jpg (20.63 Kio) Consulté 3389 fois
je me sers de ce code pour que les véhicules se mettent à la vitesse du véhicule qui le précède. Je ne vois pas comment faire autrement pour récupérer la variable "kmh" qui est la vitesse du véhicule précédent le mien ?
et effectivement si cette partie du code est gelée les clignotants fonctionnent

Code : Tout sélectionner

/*
//********************************************************
/////// Pour que  vehicule se mette à la vitesse du vehicule s'il est devant
					
					if (other.tag == "VEHICULE") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
/////// Pour que  vehicule se mette à la vitesse du velo s'il est devant

					if (other.tag == "VELO") {
						coupeDetecteur = other.GetComponent<TrajetVl>().detecteur;
						kmhActuel = other.GetComponent<TrajetVl>().kmh;  //
					}
//*************************************************
*/

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6212
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par boubouk50 » 04 Mars 2022 12:39

Je ne sais quoi te répondre Freepl.
Si tu vires ce code, alors coupeDetecteur n'est plus affecté donc il reste à false et donc les clignotants peuvent être activés (c'est la condition pour)
Donc j'en reviens à me répéter pour la 3e fois. Une des conditions est remplie et affecte coupeDetecteur à True.
Maintenant pourquoi? Ben parce que tu as une collision avec un collider d'un objet VEHICULE ou VELO vu que tu as other qui n'est pas null.
Tu dois donc avoir un collider quelque part au clonage qui te le fais changer. Mais ça, c'est dans ta maquette, ce n'est pas du code.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 04 Mars 2022 13:24

Ok
je commence à refaire tous mes modèles en vérifiant que le détecteur soit extérieur à mes vl.

Edit
J' ai donc refait tous mes véhicules en décalant le détecteur ( boule rouge) de la box collider pour eviter tout contact.
ScreenShot140.jpg
ScreenShot140.jpg (72.32 Kio) Consulté 3345 fois
et par sécurité j'utilise en gameObject avec un script de désactivation du bool que je place en début de chaque trajectoire, ainsi le bool coupeDetecteur est inactif d'office

Code : Tout sélectionner

using UnityEngine;
using System.Collections;

public class CoupeDetecteur : MonoBehaviour {
	//si true on coupe le detecteur (coche)
	//si false on ne le coupe pas (pas coche)
	public bool detecteur;
}
Coupedetecteur est bien inactif
ScreenShot141.jpg
ScreenShot141.jpg (16.47 Kio) Consulté 3345 fois
Cela semble fonctionner mais pour vérifier il faut faire tourner la mquette

Merci Boubouk50

Avatar de l’utilisateur
freepl
Messages : 1034
Inscription : 20 Mai 2012 19:33
Localisation : salon de provence

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par freepl » 04 Mars 2022 19:40

Une idee
Void onstart
Mettre detecteur == false;
Ou bien == null; en lançant ce false au bout d'une seconde par un chrono ?
Est ce jouable ?
( Envoye par mon tel , desole pour la syntaxe)

Avatar de l’utilisateur
boubouk50
ModoGenereux
ModoGenereux
Messages : 6212
Inscription : 28 Avr 2014 11:57
Localisation : Saint-Didier-en-Bresse (71)

Re: [MY-AL] Bool aléatoire à chaque création de clone ?

Message par boubouk50 » 07 Mars 2022 10:44

Toute solution qui nécessite des cas particuliers, des délais, etc. est généralement une mauvaise solution, ou une solution temporaire du moins.
Parce que dans le cas où "l'erreur" arriverait un peu plus tard ou le bon comportement pendant le délai, alors le souci perdurera. Il sera évité dans 99.99999...% des cas, mais pas dans 100%.

Maintenant, si ça fonctionne sans que cela te gêne, libre à toi. Tu n'es pas dev pro et tu ne travailles pas sur une maquette critique, donc c'est acceptable.

Maintenant, je ne sais pas si ton idée fonctionnera.
"Ce n'est pas en améliorant la bougie, que l'on a inventé l'ampoule, c'est en marchant longtemps."
Nétiquette du forum
Savoir faire une recherche
Apprendre la programmation

Répondre

Revenir vers « (C#) CSharp »