spawn an object with delay following a path

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
Avatar de l’utilisateur
Kaloverian
Messages : 336
Inscription : 10 Août 2016 03:03

spawn an object with delay following a path

Message par Kaloverian » 18 Déc 2020 20:08

bonjour,

Le seul tuto trouvé sur internet pour créer,modifier avec des courbes de Bezier un chemin afin de:faire un spawn of object following path est celui ci:
https://youtu.be/saAQNRSYU9k
avec celui-ci:
https://docs.google.com/document/d/1-FI ... 69b-Y/edit
rubrique:
Object Placement

J'ai modifié le script PathPlacement en y rajoutant un InvokeRepeating() afin de transformer ce spawn of object following path en spawn an object with delay following path.J'y suis arrivé mais il réside un problème:
le script réalisé,chaque clic de souris engendre des spawns qui ne sont pas voulus.Ce n'est pas propre.

Je cherche un tuto au sujet de:
spawn an object with delay following a path

sachant déjà que le path est réalisable avec la courbe de Bézier du 1er tuto de ce post


merci de votre aide

Avatar de l’utilisateur
Max
Messages : 8763
Inscription : 30 Juil 2011 13:57
Contact :

Re: spawn an object with delay following a path

Message par Max » 18 Déc 2020 21:19

Kaloverian a écrit :
18 Déc 2020 20:08
J'ai modifié le script PathPlacement en y rajoutant un InvokeRepeating() afin de transformer ce spawn of object following path en spawn an object with delay following path.J'y suis arrivé mais il réside un problème:
le script réalisé,chaque clic de souris engendre des spawns qui ne sont pas voulus. Ce n'est pas propre.
poste ton script, peut-être qu'il y a moyen de corriger le pb...
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
Kaloverian
Messages : 336
Inscription : 10 Août 2016 03:03

Re: spawn an object with delay following a path

Message par Kaloverian » 18 Déc 2020 22:49

1er script pour créer path:

Code : Tout sélectionner

using System.Collections.Generic;
using UnityEngine;

namespace PathCreation {
    public class PathCreator : MonoBehaviour {

        /// This class stores data for the path editor, and provides accessors to get the current vertex and bezier path.
        /// Attach to a GameObject to create a new path editor.

        public event System.Action pathUpdated;

        [SerializeField, HideInInspector]
        PathCreatorData editorData;
        [SerializeField, HideInInspector]
        bool initialized;

        GlobalDisplaySettings globalEditorDisplaySettings;

        // Vertex path created from the current bezier path
        public VertexPath path {
            get {
                if (!initialized) {
                    InitializeEditorData (false);
                }
                return editorData.GetVertexPath(transform);
            }
        }

        // The bezier path created in the editor
        public BezierPath bezierPath {
            get {
                if (!initialized) {
                    InitializeEditorData (false);
                }
                return editorData.bezierPath;
            }
            set {
                if (!initialized) {
                    InitializeEditorData (false);
                }
                editorData.bezierPath = value;
            }
        }

        #region Internal methods

        /// Used by the path editor to initialise some data
        public void InitializeEditorData (bool in2DMode) {
            if (editorData == null) {
                editorData = new PathCreatorData ();
            }
            editorData.bezierOrVertexPathModified -= TriggerPathUpdate;
            editorData.bezierOrVertexPathModified += TriggerPathUpdate;

            editorData.Initialize (in2DMode);
            initialized = true;
        }

        public PathCreatorData EditorData {
            get {
                return editorData;
            }

        }

        public void TriggerPathUpdate () {
            if (pathUpdated != null) {
                pathUpdated ();
            }
        }

#if UNITY_EDITOR

        // Draw the path when path objected is not selected (if enabled in settings)
        void OnDrawGizmos () {

            // Only draw path gizmo if the path object is not selected
            // (editor script is resposible for drawing when selected)
            GameObject selectedObj = UnityEditor.Selection.activeGameObject;
            if (selectedObj != gameObject) {

                if (path != null) {
                    path.UpdateTransform (transform);

                    if (globalEditorDisplaySettings == null) {
                        globalEditorDisplaySettings = GlobalDisplaySettings.Load ();
                    }

                    if (globalEditorDisplaySettings.visibleWhenNotSelected) {

                        Gizmos.color = globalEditorDisplaySettings.bezierPath;

                        for (int i = 0; i < path.NumPoints; i++) {
                            int nextI = i + 1;
                            if (nextI >= path.NumPoints) {
                                if (path.isClosedLoop) {
                                    nextI %= path.NumPoints;
                                } else {
                                    break;
                                }
                            }
                            Gizmos.DrawLine (path.GetPoint (i), path.GetPoint (nextI));
                        }
                    }
                }
            }
        }
#endif

        #endregion
    }
}

2ème script pour placer objet à intervalle de distance régulier sur le path:

Code : Tout sélectionner

using PathCreation;
using UnityEngine;

namespace PathCreation.Examples {

    [ExecuteInEditMode]
    public class PathPlacer : PathSceneTool {

        public GameObject prefab;
        public GameObject holder;
        public float spacing = 3;

        const float minSpacing = .1f;

        void Generate () {
            if (pathCreator != null && prefab != null && holder != null) {
                DestroyObjects ();

                VertexPath path = pathCreator.path;

                spacing = Mathf.Max(minSpacing, spacing);
                float dst = 0;

                while (dst < path.length) {
                    Vector3 point = path.GetPointAtDistance (dst);
                    Quaternion rot = path.GetRotationAtDistance (dst);
                    Instantiate (prefab, point, rot, holder.transform);
                    dst += spacing;
                }
            }
        }

        void DestroyObjects () {
            int numChildren = holder.transform.childCount;
            for (int i = numChildren - 1; i >= 0; i--) {
                DestroyImmediate (holder.transform.GetChild (i).gameObject, false);
            }
        }

        protected override void PathUpdated () {
            if (pathCreator != null) {
                Generate ();
            }
        }
    }
}
Dans ce script,la classe herite de la classe PathSceneTool.
Voici le scipt PathSceneTool:

Code : Tout sélectionner

using UnityEngine;

namespace PathCreation.Examples
{
    [ExecuteInEditMode]
    public abstract class PathSceneTool : MonoBehaviour
    {
        public event System.Action onDestroyed;
        public PathCreator pathCreator;
        public bool autoUpdate = true;

        protected VertexPath path {
            get {
                return pathCreator.path;
            }
        }

        public void TriggerUpdate() {
            PathUpdated();
        }


        protected virtual void OnDestroy() {
            if (onDestroyed != null) {
                onDestroyed();
            }
        }

        protected abstract void PathUpdated();
    }
}



PathPlacer modifié(PathPlacer_delay.cs)de façon à instancier avec un retard:

Code : Tout sélectionner

using PathCreation;
using UnityEngine;

namespace PathCreation.Examples {

	[ExecuteInEditMode]
	public class PathPlacer_delay : PathSceneTool {

		public GameObject prefab;
		public GameObject holder;
		public float spacing = 3;
		public float retard,delay;
		float dst = 0;


		const float minSpacing = .1f;

		void Start(){

			InvokeRepeating ("Generate", retard, delay);}

		void Generate () {
			if (pathCreator != null && prefab != null){ //&& holder != null) 
				//DestroyObjects ();

				VertexPath path = pathCreator.path;

				spacing = Mathf.Max(minSpacing, spacing);



				//while (dst < path.length) {
				if (dst < path.length) {
					Vector3 point = path.GetPointAtDistance (dst);
					Quaternion rot = path.GetRotationAtDistance (dst);
					Instantiate (prefab, point, rot,holder.transform);
					dst += spacing;}
				    

			}
		}

		/*void DestroyObjects () {
			int numChildren = holder.transform.childCount;
			for (int i = numChildren - 1; i >= 0; i--) {
				DestroyImmediate (holder.transform.GetChild (i).gameObject, false);
			}
		}*/

		protected override void PathUpdated () {
			if (pathCreator != null) {
				Generate ();

			}
		}
	}
}
Instructions:
Dans tous les cas,le script PathSceneTool est à conserver dans l'Asset du projet
Dans le hirarchy,ajouter:
-un prefab
-un holder(pourquoi pas choisir un empty)
-un path crée avec le script PthCreator

Dans l'Inspector:
-mettre tous les scripts
1)pour spawner following path sans retard,utiliser les scripts PathCreator,,PathPlacer
2)pour spawner following path avec un retard à chaque spawn,utiliser les scripts PathCreator,PathPlacer_delay
Pour 2),le résultat fonctionne maos n'est pas très propre.

Avatar de l’utilisateur
Kaloverian
Messages : 336
Inscription : 10 Août 2016 03:03

Re: spawn an object with delay following a path

Message par Kaloverian » 19 Déc 2020 14:38

j'ai trouvé une réponse à mon problème;
https://stackoverflow.com/questions/470 ... y-in-unity

Avatar de l’utilisateur
Max
Messages : 8763
Inscription : 30 Juil 2011 13:57
Contact :

Re: spawn an object with delay following a path

Message par Max » 19 Déc 2020 15:07

Dans ce cas, n'oublie pas de passer ton sujet en [RESOLU] .
Image
Pas d'aide par MP, le forum est là pour ça.
En cas de doute sur les bonnes pratiques à adopter sur le forum, consulter la Charte et sa FAQ

Avatar de l’utilisateur
Kaloverian
Messages : 336
Inscription : 10 Août 2016 03:03

Re: spawn an object with delay following a path

Message par Kaloverian » 19 Déc 2020 15:57

non pas résolu(code non établi)
Le dernier lien me donne seulement le chemin vers lequel je dois me diriger

Répondre

Revenir vers « (C#) CSharp »