/// <summary> /// Creates the linear template path. /// </summary> /// <param name="pm">Pm.</param> private void CreateLinearTemplatePath(PathMagic pm) { Waypoint[] wps = new Waypoint[_samples]; for (int i = 0; i < _samples; i++) { wps [i] = new Waypoint(); wps [i].position = new Vector3(_radiusX / (_samples - 1) * i, 0, 0); wps [i].symmetricTangents = _sameTangentRadius; //int nextIndex = (i + 1) % _samples; int prevIndex = ((i - 1) < 0) ? (i + _samples - 1) : (i - 1); //Vector3 nextPos = new Vector3 (_radiusX / (_samples - 1) * nextIndex, 0, 0); Vector3 prevPos = new Vector3(_radiusX / (_samples - 1) * prevIndex, 0, 0); wps [i].inTangent = (prevPos - wps [i].position).normalized * _inTangentRadius; wps [i].outTangent = -wps [i].inTangent; //(nextPos - wps [i].position).normalized * _outTangentRadius; } pm.loop = _closed; pm.waypoints = wps; }
/// <summary> /// Creates the path. The path is generated based on the path template type. /// Each time we create the path, reuse the same gamobject (_go) but destroy /// and create a new PathMagic instance (and attach it) /// </summary> private void CreateThePath() { // Create gameobject if there is no one if (_go == null) { // Register the creation in the undo system _go = new GameObject("PathMagic template"); Undo.RegisterCreatedObjectUndo(_go, "Create " + _go.name); _go.transform.rotation = Quaternion.Euler(0f, 90f, 0f); } // CHeck for PathMagic component PathMagic pm = _go.GetComponent <PathMagic> (); if (pm == null) { pm = _go.AddComponent <PathMagic> (); } Selection.activeObject = _go; if (_type == PathTemplateType.Circular) { CreateCircularTemplatePath(pm); } else if (_type == PathTemplateType.Linear) { CreateLinearTemplatePath(pm); } SceneView.RepaintAll(); }
/// <summary> /// Creates the circular template path. /// </summary> /// <param name="pm">Pm.</param> private void CreateCircularTemplatePath(PathMagic pm) { Waypoint[] wps = new Waypoint[_samples]; float angleDistance = _endAngle - _startAngle; float yAdvance = 0f; float yIncrement = _yAdvance * Mathf.Deg2Rad * angleDistance / _samples; for (int i = 0; i < _samples; i++) { wps [i] = new Waypoint(); wps [i].position = new Vector3(_radiusX * Mathf.Sin(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * i), yAdvance, _radiusY * Mathf.Cos(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * i)); // Compute prev and next index int nextIndex = (i + 1) % _samples; int prevIndex = ((i - 1) < 0) ? (i + _samples - 1) : (i - 1); // Compute prev and next positions Vector3 nextPos = new Vector3(_radiusX * Mathf.Sin(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * nextIndex), yAdvance + yIncrement, _radiusY * Mathf.Cos(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * nextIndex)); Vector3 prevPos = new Vector3(_radiusX * Mathf.Sin(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * prevIndex), yAdvance - yIncrement, _radiusY * Mathf.Cos(Mathf.Deg2Rad * _startAngle + _periods * Mathf.Deg2Rad * angleDistance / _samples * prevIndex)); // At end of path clear yAdvance effect if (nextIndex == 0) { nextPos.y = 0; } // Tangent modes if (_tangentMode == TangentMode.InPath) { // In this case we create tangents oriented to other waypoints // This has a polygon effect wps [i].symmetricTangents = false; wps [i].inTangent = (prevPos - wps [i].position).normalized * _inTangentRadius; wps [i].outTangent = (nextPos - wps [i].position).normalized * _outTangentRadius; } else if (_tangentMode == TangentMode.Tangent) { // We want to compute tangents to create a circular effect // we compute the "normal" for points wp, center and wp + 1up wps [i].symmetricTangents = false; Vector3 side1 = Vector3.up; Vector3 side2 = -wps [i].position; wps [i].inTangent = _inTangentRadius * Vector3.Cross(side1, side2).normalized; wps [i].outTangent = _outTangentRadius * Vector3.Cross(side2, side1).normalized; // Now we consider effect of yAdvance to create spirals float dist; float xyDistance; float tangentAngle; // At begin and end of the spiral we want in/out tangents // symmetric with counterparts if (i > 0) { dist = Vector3.Distance(wps [i].position, prevPos); xyDistance = Mathf.Sqrt(dist * dist - yIncrement * yIncrement); tangentAngle = Mathf.Atan2(xyDistance, yIncrement); wps [i].inTangent -= _inTangentRadius * Mathf.Cos(tangentAngle) * Vector3.up; } if (i < (_samples - 1)) { dist = Vector3.Distance(wps [i].position, nextPos); xyDistance = Mathf.Sqrt(dist * dist - yIncrement * yIncrement); tangentAngle = Mathf.Atan2(xyDistance, yIncrement); wps [i].outTangent += _outTangentRadius * Mathf.Cos(tangentAngle) * Vector3.up; } else { wps [i].outTangent = -wps [i].inTangent; } if (i == 0) { wps [i].inTangent = -wps [i].outTangent; } } // Increment ascent yAdvance += yIncrement; } pm.loop = _closed; pm.waypoints = wps; }