Esempio n. 1
0
        static void DrawSplineGizmo(BezierSplineAdvanced spline, GizmoType gizmoType)
        {
            if (spline.Count < 2)
            {
                return;
            }

            Gizmos.color = SPLINE_GIZMO_COLOR;

            Vector3 lastPos        = spline[0].position;
            float   increaseAmount = 1f / (spline.Count * SPLINE_GIZMO_SMOOTHNESS);

            for (float i = increaseAmount; i < 1f; i += increaseAmount)
            {
                Vector3 pos = spline.GetPoint(i);
                Gizmos.DrawLine(lastPos, pos);
                lastPos = pos;
            }

            Gizmos.DrawLine(lastPos, spline.GetPoint(1f));
        }
        private void Update()
        {
            cachedTransform.position = Vector3.Lerp(cachedTransform.position, spline.GetPoint(progress), movementLerpModifier * Time.deltaTime);

            if (lookForward)
            {
                Quaternion targetRotation;
                if (isGoingForward)
                {
                    targetRotation = Quaternion.LookRotation(spline.GetTangent(progress));
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-spline.GetTangent(progress));
                }

                cachedTransform.rotation = Quaternion.Lerp(cachedTransform.rotation, targetRotation, rotationLerpModifier * Time.deltaTime);
            }

            if (isGoingForward)
            {
                progress += Time.deltaTime / travelTime;

                if (progress > 1f)
                {
                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt1 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress -= 1f;
                    }
                    else
                    {
                        progress       = 2f - progress;
                        isGoingForward = false;
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                progress -= Time.deltaTime / travelTime;

                if (progress < 0f)
                {
                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt0 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress += 1f;
                    }
                    else
                    {
                        progress       = -progress;
                        isGoingForward = true;
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
Esempio n. 3
0
        private void LateUpdate()
        {
            if (spline == null || cachedPS == null)
            {
                return;
            }

            if (particles.Length < cachedMainModule.maxParticles && particles.Length < MAX_PARTICLE_COUNT)
            {
                particles = new ParticleSystem.Particle[Mathf.Min(cachedMainModule.maxParticles, MAX_PARTICLE_COUNT)];
            }

            bool isLocalSpace   = cachedMainModule.simulationSpace != ParticleSystemSimulationSpace.World;
            int  aliveParticles = cachedPS.GetParticles(particles);

            if (followMode == FollowMode.Relaxed)
            {
                if (particleData == null)
                {
                    particleData = new List <Vector4>(particles.Length);
                }

                cachedPS.GetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);

                // Credit: https://forum.unity3d.com/threads/access-to-the-particle-system-lifecycle-events.328918/#post-2295977
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector4 particleDat = particleData[i];
                    Vector3 point       = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime));
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    // Move particles alongside the spline
                    if (particleDat.w != 0f)
                    {
                        particles[i].position += point - (Vector3)particleDat;
                    }

                    particleDat     = point;
                    particleDat.w   = 1f;
                    particleData[i] = particleDat;
                }

                cachedPS.SetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);
            }
            else
            {
                Vector3 deltaPosition = cachedTransform.position - spline.GetPoint(0f);
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector3 point = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime)) + deltaPosition;
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    particles[i].position = point;
                }
            }

            cachedPS.SetParticles(particles, aliveParticles);
        }