/// <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);
                }
            }
        }
Exemple #2
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>
        /// Follow currentSpline path
        /// </summary>
        private void FollowSpline()
        {
            if (currentSpline == null)
            {
                return;
            }

            _goingForward  = (speed > 0);
            _cicleDuration = speed == 0f ? 0f : _distance / Mathf.Abs(speed);

            if (_goingForward)
            {
                if (!playingTrackSound)
                {
                    playingTrackSound = true;
                    // Play sound
                    GameObject.Find("TrackSound").GetComponent <ClickSound>().PlaySound("TrackSound");
                }
                if (_cicleDuration > 0f)
                {
                    _progress += Time.deltaTime / _cicleDuration;
                }

                if (_progress > 1f)
                {
                    switch (mode)
                    {
                    case SplineFollowerMode.StopAtTheEnd:
                        _progress = 1f;
                        break;

                    case SplineFollowerMode.Loop:
                        _progress -= 1f;
                        break;

                    case SplineFollowerMode.PingPong:
                        _progress = 1f;
                        speed    *= -1;
                        break;
                    }
                }
            }
            else
            {
                if (_cicleDuration > 0f)
                {
                    _progress -= Time.deltaTime / _cicleDuration;
                }

                if (_progress < 0f)
                {
                    switch (mode)
                    {
                    case SplineFollowerMode.StopAtTheEnd:
                        _progress = 0f;
                        break;

                    case SplineFollowerMode.Loop:
                        _progress += 1f;
                        break;

                    case SplineFollowerMode.PingPong:
                        _progress = 0;
                        speed    *= -1;
                        break;
                    }
                }
            }
            Vector3 position = currentSpline.GetPoint(_progress);

            transform.localPosition = Vector3.Lerp(transform.localPosition, position, 1f);
            Vector3 LookPos = position + currentSpline.GetDirection(_progress);

            transform.LookAt(LookPos);



            // Rotate the cube by converting the angles into a quaternion.
            // Quaternion reverseRotation = new Quaternion(0, 0, 180.0f);



            if (derivative(_progress).x < 0)
            {
                GameObject.Find("LuggCart").transform.localScale = new Vector3(1, -1, 1);
                try
                {
                    Vector3 rot = GameObject.Find("First Person Camera").transform.rotation.eulerAngles;
                    rot = new Vector3(rot.x, rot.y, 180);
                    GameObject.Find("First Person Camera").transform.rotation = Quaternion.Euler(rot);
                }
                catch (System.NullReferenceException)
                {
                }
            }
            else
            {
                GameObject.Find("LuggCart").transform.localScale = new Vector3(1, 1, 1);

                try
                {
                    Vector3 rot = GameObject.Find("First Person Camera").transform.rotation.eulerAngles;
                    rot = new Vector3(rot.x, rot.y, 0);
                    GameObject.Find("First Person Camera").transform.rotation = Quaternion.Euler(rot);
                }
                catch (System.NullReferenceException)
                {
                }
            }
            // TODO: fix line 135.
            // this line determines the position of the tranform. try to modify it!
        }
        /// <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;
                }
            }
        }