Salut tous le monde, je fais un script. En gros, il doit permettre à des IA de suivre un chemin grâce à système waypoint
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EiMovement : MonoBehaviour {
public static Transform[] wayPoints;
public Transform target;
public float speed;
private int wayPointIndex;
// Use this for initialization
void Awake () {
wayPoints = new Transform[transform.childCount];
for (int i = 0; i < wayPoints.Length; i++) {
wayPoints[i] = transform.GetChild (i);
}
}
void Start(){
target = wayPoints[0];
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - transform.position;
transform.Translate (dir.normalized * speed * Time.deltaTime * speed);
if (Vector3.Distance(transform.position, target.position) <= 0.2f){
goNextWayPoint ();
}
}
void goNextWayPoint(){
if (wayPointIndex == wayPoints.Length -1) {
Destroy (gameObject);
return;
}
wayPointIndex++;
target = wayPoints[wayPointIndex];
}
}
J'ai une erreur, array out of range, j'ai essayé de résoudre ça mais je n'arrive pas ;(
Si quelqu'un pouvait m'aider ?
- Edité par JeanClaudeGoldenBo 4 août 2017 à 23:03:58
Je n'utilises pas Unity et je ne développe pas en C# mais je pense que tu as un problème d'initialisation.
Afin de pouvoir t'aider, quand est appelée la fonction Awake et quand est appelée la fonction start ? Il semblerait que la fonction start soit appelée avant la fonction Awake ce qui expliquerait que tu n'aies rien à l'indice 0 du tableau.
Une autre hypothèse serait que transform.childCount vaille 0 et donc ta boucle d'initialisation ne servirai à rien.
"The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed. Awake is always called before any Start functions. This allows you to order initialization of scripts."
En français :
La différence entre Awake et Start est que Start n'est appelée que si l'instance de script est activée. Cela vous permet de retarder tout code d'initialisation, jusqu'à ce qu'il soit vraiment nécessaire. Awake est toujours appelé avant toute fonction Start. Cela vous permet de commander l'initialisation des scripts.
Et (je n'y connais rien alors ne me jugez pas ) tu ne peux pas faire ça ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EiMovement : MonoBehaviour {
public static Transform[] wayPoints = new Transform[transform.childCount];
public Transform target;
public float speed;
private int wayPointIndex;
// Use this for initialization
void Awake () {
for (int i = 0; i < wayPoints.Length; i++) {
wayPoints[i] = transform.GetChild (i);
}
}
void Start(){
target = wayPoints[0];
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - transform.position;
transform.Translate (dir.normalized * speed * Time.deltaTime * speed);
if (Vector3.Distance(transform.position, target.position) <= 0.2f){
goNextWayPoint ();
}
}
void goNextWayPoint(){
if (wayPointIndex == wayPoints.Length -1) {
Destroy (gameObject);
return;
}
wayPointIndex++;
target = wayPoints[wayPointIndex];
}
}
Merci comme même de l'aide mais mettre comme çà sa ne marche pas, j'ai enlever la ligne 27 et sa merche (à moitier ;( ..)
L'<enemy> avance vers le premier waypoint, mais s'arrete et ne poursuit pas vers le 2eme waypoint.. Je vais essayé de trouver comme le faire changer de <target> à chaque fois qu'il attend une d'elle
New :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EiMovement : MonoBehaviour {
public static Transform[] wayPoints;
public Transform target;
public float speed;
private int wayPointIndex = 0;
// Use this for initialization
void Awake () {
wayPoints = new Transform[transform.childCount];
for (int i = 0; i < wayPoints.Length; i++) {
wayPoints[i] = transform.GetChild (i);
}
}
void Start(){
//target = wayPoints[0];
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - transform.position;
transform.Translate (dir.normalized * speed * Time.deltaTime);
if (Vector3.Distance(transform.position, target.position) <= 0.2f){
goNextWayPoint ();
}
}
void goNextWayPoint(){
target = wayPoints[wayPointIndex];
wayPointIndex++;
/*if (wayPointIndex == wayPoints.Length - 1) {
target = wayPoints[wayPointIndex];
wayPointIndex++;
//Destroy (gameObject);
//return;
}
wayPointIndex++;
target = wayPoints[wayPointIndex];*/
}
}
- Edité par JeanClaudeGoldenBo 5 août 2017 à 3:36:45
J'ai mis la partie Awake() dans un autre script puis j'y accède depuis le script principal script et sa marche
Script 1 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaypointInisalize : MonoBehaviour {
public static Transform[] wayPoints;
// Use this for initialization
void Start () {
wayPoints = new Transform[transform.childCount];
for (int i = 0; i < wayPoints.Length; i++) {
wayPoints[i] = transform.GetChild (i);
}
}
// Update is called once per frame
void Update () {
}
}
Script 2 : (*Script principal*)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EiMovement : MonoBehaviour {
private Transform target;
public float speed;
private int wayPointIndex = 0;
// Use this for initialization
void Awake () {
}
void Start(){
target = WaypointInisalize.wayPoints[0];
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - transform.position;
transform.Translate (dir.normalized * speed * Time.deltaTime);
if (Vector3.Distance(transform.position, target.position) <= 0.2f){
goNextWayPoint ();
}
}
void goNextWayPoint(){
wayPointIndex++;
target = WaypointInisalize.wayPoints[wayPointIndex];
/*if (wayPointIndex == wayPoints.Length - 1) {
target = wayPoints[wayPointIndex];
wayPointIndex++;
//Destroy (gameObject);
//return;
}
wayPointIndex++;
target = wayPoints[wayPointIndex];*/
}
}
Merci d'avoir pris la peine de me réponde
- Edité par JeanClaudeGoldenBo 6 août 2017 à 0:37:57
Citatione
[Unity Intermédiaire]Array out of range
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
Blog - GitHub
Blog - GitHub
Blog - GitHub