Beispiel #1
0
        /// <summary>
        /// Create new renderer at the end of the current one
        /// </summary>
        public GameObject ConnectNewRenderer()
        {
            Vector3    lastPointPosition = _spline.GetControlPointPosition(_spline.ControlPointCount - 1);
            Quaternion lastPointRotation = _spline.GetRotation(1);

            Vector3    position = transform.TransformPoint(lastPointPosition);
            GameObject clone    = Instantiate(this.gameObject, position, _spline.GetRotation(1) * Quaternion.LookRotation(_spline.GetDirection(1)));

            Spline newRendererSpline = clone.GetComponent <Spline>();

            newRendererSpline.Reset();
            newRendererSpline.ResetRotations(lastPointRotation);

            SplineMeshRenderer newRendererSplineMeshRenderer = clone.GetComponent <SplineMeshRenderer>();

            newRendererSplineMeshRenderer.realtimeMeshGeneration = realtimeMeshGeneration;
            newRendererSplineMeshRenderer.ExtrudeMesh();

            return(clone);
        }
        /// <summary>
        /// Spawn prefabs along spline
        /// </summary>
        public void SpawnPrefabs()
        {
            ResetObjects();

            if (frequency <= 0 || prefabs == null || prefabs.Length == 0)
            {
                return;
            }

            float stepSize = frequency * prefabs.Length;

            if (spline.Loop || stepSize == 1)
            {
                stepSize = 1f / stepSize;
            }
            else
            {
                stepSize = 1f / (stepSize - 1);
            }

            for (int p = 0, f = 0; f < frequency; f++)
            {
                for (int i = 0; i < prefabs.Length; i++, p++)
                {
                    GameObject newClone      = Instantiate(prefabs[i]);;
                    Vector3    pointPosition = spline.GetPoint(p * stepSize);
                    Quaternion pointRotation = spline.GetRotation(p * stepSize);

                    newClone.transform.localPosition = pointPosition;

                    newClone.transform.LookAt(pointPosition + spline.GetDirection(p * stepSize));
                    newClone.transform.rotation *= pointRotation;

                    newClone.transform.parent = transform;

                    clones.Add(newClone);
                }
            }
        }
        /// <summary>
        /// Spawn prefabs along spline
        /// </summary>
        public void SpawnPrefabs()
        {
            if (spline == null)
            {
                Debug.Log("Please select a reference spline to spawn prefabs.");
                return;
            }

            ResetObjects();

            instances = Mathf.Abs(instances);

            if (instances <= 0 || prefabs == null || prefabs.Length == 0)
            {
                return;
            }

            float stepSize = instances * prefabs.Length;
            float t;

            // if loop does not spawn a double at the end
            stepSize = (spline.Loop || stepSize == 1) ? (1f / stepSize) : (1f / (stepSize - 1));

            GameObject newClone;
            Vector3    clonePosition;
            Quaternion cloneRotation;
            Vector3    cloneDirection;

            for (int positionIndex = 0, instanceIndex = 0; instanceIndex < instances; instanceIndex++)
            {
                for (int prefabIndex = 0; prefabIndex < prefabs.Length; prefabIndex++, positionIndex++)
                {
                    newClone = Instantiate(prefabs[prefabIndex]);
                    t        = positionIndex * stepSize;

                    if (spline.FollowTerrain)
                    {
                        ValidateOrientedPoints();

                        int index = spline.GetClosestOrientedPointIndex(t);
                        clonePosition = spline.OrientedPoints[index].Position;
                        cloneRotation = spline.OrientedPoints[index].Rotation;

                        int nextIndex = index + 1;

                        if (nextIndex > spline.OrientedPoints.Length - 1)
                        {
                            if (spline.Loop)
                            {
                                nextIndex = 0;
                            }
                            else
                            {
                                nextIndex = index;
                                index--;
                            }
                        }

                        cloneDirection = (spline.OrientedPoints[nextIndex].Position - spline.OrientedPoints[index].Position).normalized;
                    }
                    else
                    {
                        clonePosition  = spline.GetPoint(t) + spawnOffset;
                        cloneRotation  = spline.GetRotation(t);
                        cloneDirection = spline.GetDirection(t);
                    }

                    //cloneDirection = spline.GetDirection(t);

                    newClone.transform.localPosition = clonePosition;

                    newClone.transform.rotation = cloneRotation;
                    newClone.transform.LookAt(clonePosition + cloneDirection, newClone.transform.up);

                    newClone.transform.parent = transform;
                }
            }
        }