au cas ou je vous met mon script Perso (le vrai si jamais sa peut aider a mieux saisir ce que je veux et mes problèmes ...
mais attention il est sale (j’expérimente donc c'est pas très claire désolé
)
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Persos {
public enum actionEnCours {attendre, choisirZone, Marcher}
public enum races {koala, ecureuil, chacal, loup, requin, loutre, marmotte}
public Color actualCoulleur;
//public Color c1, c2, c3, c4, c5, c6, c7;
public Color[] couleursCorp = new Color[7];
public races race;
public actionEnCours act;
public GameObject[] zoneEau;
public GameObject[] zoneNourriture;
public string prenom;
public string alliPerso;
public List <GameObject> ennemi = new List<GameObject>();
public Vector3 zoneChoisi;
public int vie;
public int vitesse;
public int soif;
public int soifMax = 100;
public bool aSoif = false;
public int faim;
public int faimMax = 100;
public bool aFaim = false;
public int argentSurSoi = 100;
float decompte = 1f;
public float chrono = 0;
public float attente;
public float decompte2 = 10f;
public bool canAtack = true;
public float distRay = 10f;
public void Lancement(GameObject t){
ChoixAlli (t);
AttributionCouleur ();
SpecificiteRace (t);
RecupZoneAExplo ();
}
public void Attaque(GameObject t){
if (canAtack) {
RaycastHit hit;
Vector3 pos = t.transform.position;
Vector3 fwd = t.transform.TransformDirection (Vector3.forward);
Ray ray = new Ray (pos, fwd);
if (Physics.Raycast (ray, out hit, distRay)) {
Debug.DrawRay (pos, fwd * 10, Color.red);
if (hit.rigidbody != null) {
for (int i = 0; i < ennemi.Count; i++) {
if (hit.rigidbody.gameObject == ennemi [i]) {
Debug.Log ("Attaque");
hit.rigidbody.GetComponent <Hero>().hero.vie --;
}
}
}
}
}
}
public void Scanenemy(){
for (int i = 0; i < ennemi.Count; i++) {
if (ennemi [i] == null) {
ennemi.RemoveAt (i);
}
}
}
public void ChoixAlli(GameObject t){
BaseAlliance aLib;
int rng = Random.Range (0, AllianceManager.Instance.alliT.Count);
alliPerso = AllianceManager.Instance.alliT[rng].allianceName;
aLib = AllianceManager.Instance.alliT [rng];
aLib.nbreDeMembres++;
if (alliPerso == "Nouvelle Aldes") {
aLib.memDeLAlli.Add (t);
t.tag = "NewAldes";
}else if (alliPerso == "Phoenix") {
aLib.memDeLAlli.Add (t);
t.tag = "Phoenix";
}else if (alliPerso == "Tesla") {
aLib.memDeLAlli.Add (t);
t.tag = "Tesla";
}
}
public void TestEtat(GameObject t){
Scanenemy ();
if (act == actionEnCours.attendre) {
Delay ();
} else if (act == actionEnCours.choisirZone) {
ChoisirZoneExplorable(t);
} else if (act == actionEnCours.Marcher) {
Marcher (t);
}
}
public void ConsomeRess3(){
if (decompte2 > 0) {
decompte2 -= Time.deltaTime;
} else if (decompte2 < 0) {
faim-= Random.Range (1, 4);
soif-= Random.Range (1, 4);
AEnvieDe (soif, faim);
decompte2 = 10f;
}
}
public void AEnvieDe(int ress1, int ress2){
if (ress1 <= 35) {
aSoif = true;
} else {
aSoif = false;
}
if (ress2 <= 35) {
aFaim = true;
} else {
aFaim = false;
}
}
public Vector3 RngZone(){
return new Vector3 (Random.Range (0, 101), 1f, Random.Range (0, 101));
}
public void RecupZoneAExplo(){
zoneEau = GameObject.FindGameObjectsWithTag ("Eau");
zoneNourriture = GameObject.FindGameObjectsWithTag ("Nouriture");
}
public void RecupEnnemy(){
if (alliPerso == "Nouvelle Aldes") {
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("Phoenix"));
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("Tesla"));
}else if (alliPerso == "Phoenix") {
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("Tesla"));
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("NewAldes"));
}else if (alliPerso == "Tesla") {
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("NewAldes"));
ennemi.AddRange (GameObject.FindGameObjectsWithTag ("Phoenix"));
}
}
public void ChoixZoneMini (GameObject go, GameObject[] zonestest) {
Vector3 position = go.transform.position;
int i;
float mini = 50000;
for (i = 0; i < zonestest.Length; i++) {
Vector3 diff = zonestest[i].transform.position - position;
float curDistance = diff.sqrMagnitude;
if (curDistance < mini) {
mini = curDistance;
Vector3 zoneChoisi2 = zonestest [i].transform.position;
zoneChoisi = new Vector3 (zoneChoisi2.x, 1.0f, zoneChoisi2.z);
}
}
}
public void ChoisirZoneExplorable(GameObject go){
chrono = 0f;
attente = Random.Range (1.5f, 5.1f);
if (aSoif) {
ChoixZoneMini (go, zoneEau);
} else if (aFaim) {
ChoixZoneMini (go, zoneNourriture);
} else {
zoneChoisi = RngZone ();
}
act = actionEnCours.Marcher;
}
public void Marcher(GameObject t){
if (act == actionEnCours.Marcher) { // Si on a choisi une zone on y va (zoneChoisi != null)
t.transform.LookAt (zoneChoisi);
t.transform.position = Vector3.MoveTowards (t.transform.position, zoneChoisi, Time.deltaTime * vitesse);
if (CheckDistance (t) <= 1f) {
act = actionEnCours.attendre;
}
}
}
public float CheckDistance(GameObject t){
return Vector3.Distance (t.transform.position, zoneChoisi);
}
public void Delay () {
if (chrono < attente) {
if (decompte > 0) {
decompte -= Time.deltaTime;
} else if (decompte < 0) {
chrono++;
decompte = 1f;
}
} else {
//Debug.Log ("Test reussi !!!");
act = actionEnCours.choisirZone;
}
}
public float conversion (float b){
return b / 255;
}
public void AttributionCouleur(){
couleursCorp [0] = new Color (conversion (255), conversion (255), conversion (255), conversion (255));
couleursCorp [1] = new Color (conversion (122), conversion (61), conversion (6), conversion (255));
couleursCorp [2] = new Color (conversion (94), conversion (94), conversion (94), conversion (255));
couleursCorp [3] = new Color (conversion (206), conversion (205), conversion (205), conversion (255));
couleursCorp [4] = new Color (conversion (116), conversion (105), conversion (251), conversion (255));
couleursCorp [5] = new Color (conversion (118), conversion (115), conversion (16), conversion (255));
couleursCorp [6] = new Color (conversion (251), conversion (194), conversion (194), conversion (255));
}
public void SpecificiteRace(GameObject t){
Renderer rend = t.GetComponent <Renderer> ();
race = (races)Random.Range (0, 7);
if (race == races.koala) {
vie =5;
vitesse =2;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[0];
rend.material.color = actualCoulleur;
} else if (race == races.ecureuil) {
vie =5;
vitesse =4;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[1];
rend.material.color = actualCoulleur;
}else if (race == races.chacal) {
vie =5;
vitesse =5;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[2];
rend.material.color = actualCoulleur;
}else if (race == races.loup) {
vie =6;
vitesse =7;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[3];
rend.material.color = actualCoulleur;
}else if (race == races.requin) {
vie =6;
vitesse =6;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[4];
rend.material.color = actualCoulleur;
}else if (race == races.loutre) {
vie =6;
vitesse =3;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[5];
rend.material.color = actualCoulleur;
}else if (race == races.marmotte) {
vie =4;
vitesse =2;
soif = Random.Range (30, 101);
faim = Random.Range (30, 101);
actualCoulleur = couleursCorp[6];
rend.material.color = actualCoulleur;
}
}
public Persos (){
}
}
et du coup pour instancier ces perso en vrai je met le script ci-dessous sur un prefab et j'instancie le prefab sur le dernier script en bas
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
public class Hero : MonoBehaviour {
public Persos hero;
private Color myColor;
public bool isEnnemy = false;
// Use this for initialization
void Awake () {
hero = new Persos ();
hero.Lancement (this.gameObject);
hero.act = Persos.actionEnCours.choisirZone;
nommage ();
}
void Start(){
hero.RecupEnnemy ();
}
public void nommage(){
hero.prenom = Nommage.nomme ();
Debug.Log (Nommage.prenomMasculin.Count);
}
public void Dead(){
if (hero.vie <= 0) {
Debug.Log ("Je suis mort " + this.gameObject.name);
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
hero.ConsomeRess3 ();
hero.TestEtat (this.gameObject);
hero.Attaque (this.gameObject);
Dead ();
}
}
et enfin:
Code : Tout sélectionner
using UnityEngine;
using System.Collections;
public class GenGame : MonoBehaviour {
public GameObject eau; // Point d'eau
public GameObject nourriture; // Point de nouriture
public GameObject Balli;
public int nEau, nNou, nCorp, nBa; // Nombre de point de ressources (eaus, nouritures, joueurs)
int mapsizeX; // Taille de la map vecteur X
int mapsizeZ; // Taille de la map vecteur Z
public int[,] map; // Tableau de vecteur X.Z etat de la case...
public GameObject heroR; // Rangement dans la hierarchie du projet (un R a la fin)
public GameObject solsR;
public GameObject eauR;
public GameObject nourritureR;
public GameObject BalliR;
public GameObject corp; // Corp du joueurs
public GameObject go1;
public void Awake() {
TileMap tm = go1.GetComponent <TileMap> ();
mapsizeX = (int)tm.tileSize * tm.size_x;
mapsizeZ = (int)tm.tileSize * tm.size_z;
}
public void Start() {
GenerateMap ();
DefNombreRessources ();
GenerateRessources (eau, nEau);
GenerateRessources (nourriture, nNou);
GenerateRessources (Balli, nBa);
GenerateHero (corp, nCorp);
}
public void GenerateMap(){
map = new int[mapsizeX, mapsizeZ];
for (int x = 0; x < mapsizeX; x++) {
for (int z = 0; z < mapsizeZ; z++) {
map [x, z] = 0;
}
}
}
public void DefNombreRessources() {
nEau = Random.Range (1, 6);
nNou = Random.Range (1, 6);
nCorp = Random.Range (12, 26);
nBa = 3;
}
public void GenerateHero(GameObject go, int n){
float y = 1.0f;
bool ressPosee = false;
for (int i = 0; i < n; i++) {
ressPosee = false;
while (ressPosee == false) {
int x = Random.Range (3, mapsizeX-2 );
int z = Random.Range (3, mapsizeZ-2 );
if (map [x, z] == 0) {
GameObject go2 = Instantiate (go, new Vector3 (x, y, z), Quaternion.identity)as GameObject;
ressPosee = true;
map [x, z] = 3;
go2.transform.parent = heroR.transform;
}
}
}
}
public void GenerateRessources(GameObject go, int n) {
float y = 0;
bool ressPosee = false;
if (go == eau) {
y = 0.25f;
} else if (go == nourriture) {
y = 0.5f;
} else if (go == Balli) {
y = 0.5f;
}
for (int i = 0; i < n; i++) {
ressPosee = false;
while (ressPosee == false) {
int x = Random.Range (3, mapsizeX-2 );
int z = Random.Range (3, mapsizeZ-2 );
if (map [x, z] == 0) {
GameObject t1 = Instantiate (go, new Vector3 (x, y, z), Quaternion.identity)as GameObject;
map [x, z] = 1;
ressPosee = true;
if (go.name == eau.name) {
t1.transform.parent = eauR.transform;
} else if (go.name == nourriture.name) {
t1.transform.parent = nourritureR.transform;
}else if (go.name == Balli.name) {
t1.transform.parent = BalliR.transform;
}
}
}
}
}
}
avec cette version tout fonctionne mais j'aime pas la maniere dont je creer les perso je prefere la maniere que je vous ai montrer en exemeple mais sa marche pas ^^ du coup je sais pas quoi faire continuer comme je fais ou reussir a comprendre comment faire au mieux