/// <summary> /// Get a list of spline oriented points based on the number os steps /// </summary> /// <param name="steps"></param> /// <returns></returns> public List <OrientedPoint> GetOrientedPoints(int steps) { List <OrientedPoint> ret = new List <OrientedPoint>(); float stepPercentage = 1f / steps; float t = 0; while (t < 1f) { OrientedPoint orientedPoint = GetOrientedPoint(t); ret.Add(orientedPoint); t += stepPercentage; } return(ret); }
/// <summary> /// Calculate mesh generation parameters /// </summary> private void CreateMesh() { if (_auxTransform1 != null) { GetAuxTranforms(); } if (_auxTransform1 == null) { return; } float totalDistance = _spline.GetTotalDistance(); for (float dist = 0.0f; dist < totalDistance; dist += _baseMeshLength) { float initialT = Mathf.Clamp01(dist / totalDistance); float finalT = Mathf.Clamp01((dist + _baseMeshLength) / totalDistance); OrientedPoint startPoint = _spline.GetOrientedPoint(initialT); OrientedPoint endPoint = _spline.GetOrientedPoint(finalT); _auxTransform1.rotation = startPoint.Rotation; _auxTransform1.position = startPoint.Position + meshOffset; _auxTransform2.rotation = endPoint.Rotation; _auxTransform2.position = endPoint.Position + meshOffset; bool exceededVertsLimit = false; bool notUVMapped = false; AddSegment(_auxTransform1, _auxTransform2, out exceededVertsLimit, out notUVMapped); if (notUVMapped) { break; } if (exceededVertsLimit) { realtimeMeshGeneration = false; break; } } }
/// <summary> /// Move object to start position /// </summary> public void MoveToStartPosition() { if (splines == null || splines.Count == 0) { return; } float t = customStartPosition * 0.01f; splines[0].CalculateOrientedPoints(1f); int index = (int)(splines[0].OrientedPoints.Length * t); OrientedPoint start = splines[0].OrientedPoints[index]; transform.position = start.Position; if (applySplineRotation) { transform.rotation = start.Rotation; } }
/// <summary> /// Follow spline path /// </summary> private void FollowSpline() { if (splines == null || splines.Count == 0) { return; } _cicleDuration = speed == 0f ? 0f : _distance / Mathf.Abs(speed); if (_goingForward) { if (_cicleDuration > 0f) { _progress += (Time.deltaTime / _cicleDuration); } if (_progress > 1f) { if (_currentSpline < splines.Count - 1) { _currentSpline++; _progress = 0; RecalculateCicleDuration(); if (cycleEndStops == SplineFollowerStops.EachSpline) { _currentStopTime = cycleStopTime; } } else //Reached end of spline list { switch (followerBehaviour) { case SplineFollowerBehaviour.StopAtTheEnd: _progress = 1f; break; case SplineFollowerBehaviour.Loop: _currentSpline = 0; _progress -= 1f; RecalculateCicleDuration(); break; case SplineFollowerBehaviour.BackAndForward: _progress = 1f; speed *= -1; break; } if (cycleEndStops == SplineFollowerStops.LastSpline || cycleEndStops == SplineFollowerStops.EachSpline) { _currentStopTime = cycleStopTime; } } } } else { if (_cicleDuration > 0f) { _progress -= (Time.deltaTime / _cicleDuration); } if (_progress < 0f) { if (_currentSpline > 0) { _currentSpline--; _progress = 1; RecalculateCicleDuration(); if (cycleEndStops == SplineFollowerStops.EachSpline) { _currentStopTime = cycleStopTime; } } else //Reached end of spline list { switch (followerBehaviour) { case SplineFollowerBehaviour.StopAtTheEnd: _progress = 0f; break; case SplineFollowerBehaviour.Loop: _currentSpline = splines.Count - 1; _progress += 1f; RecalculateCicleDuration(); break; case SplineFollowerBehaviour.BackAndForward: _progress = 0; speed *= -1; break; } if (cycleEndStops == SplineFollowerStops.LastSpline || cycleEndStops == SplineFollowerStops.EachSpline) { _currentStopTime = cycleStopTime; } } } } OrientedPoint orientedPoint = splines[_currentSpline].GetOrientedPoint(_progress); _currentPosition = orientedPoint.Position; _currentRotation = orientedPoint.Rotation; _lookDirection = _currentPosition + splines[_currentSpline].GetDirection(_progress); transform.position = Vector3.Lerp(transform.position, _currentPosition, 1f); if (applySplineRotation) { transform.rotation = _currentRotation; } }