internal static void DrawPathGizmo(CinemachinePathBase path, Color pathColor) { // Draw the path var colorOld = Gizmos.color; Gizmos.color = pathColor; var step = 1f / path.m_Resolution; var lastPos = path.EvaluatePosition(path.MinPos); var lastW = path.EvaluateOrientation(path.MinPos) * Vector3.right * path.m_Appearance.width / 2; for (var t = path.MinPos + step; t <= path.MaxPos + step / 2; t += step) { var p = path.EvaluatePosition(t); var q = path.EvaluateOrientation(t); var w = q * Vector3.right * path.m_Appearance.width / 2; var w2 = w * 1.2f; var p0 = p - w2; var p1 = p + w2; Gizmos.DrawLine(p0, p1); Gizmos.DrawLine(lastPos - lastW, p - w); Gizmos.DrawLine(lastPos + lastW, p + w); #if false // Show the normals, for debugging Gizmos.color = Color.red; Vector3 y = (q * Vector3.up) * width / 2; Gizmos.DrawLine(p, p + y); Gizmos.color = pathColor; #endif lastPos = p; lastW = w; } Gizmos.color = colorOld; }
public static void DrawPathGizmo(CinemachinePathBase path, Color pathColor) { // Draw the path Color colorOld = Gizmos.color; Gizmos.color = pathColor; float step = 1f / path.m_Resolution; Vector3 lastPos = path.EvaluatePosition(path.MinPos); Vector3 lastW = (path.EvaluateOrientation(path.MinPos) * Vector3.right) * path.m_Appearance.width / 2; for (float t = path.MinPos + step; t <= path.MaxPos + step / 2; t += step) { Vector3 p = path.EvaluatePosition(t); Quaternion q = path.EvaluateOrientation(t); Vector3 w = (q * Vector3.right) * path.m_Appearance.width / 2; Vector3 w2 = w * 1.2f; Vector3 p0 = p - w2; Vector3 p1 = p + w2; Gizmos.DrawLine(p0, p1); Gizmos.DrawLine(lastPos - lastW, p - w); Gizmos.DrawLine(lastPos + lastW, p + w); #if false // Show the normals, for debugging Gizmos.color = Color.red; Vector3 y = (q * Vector3.up) * path.m_Appearance.width / 2; Gizmos.DrawLine(p, p + y); Gizmos.color = pathColor; #endif lastPos = p; lastW = w; } Gizmos.color = colorOld; }
public PathMeshNode(CinemachinePathBase path, float time) { Vector3 center = path.EvaluatePosition(time); Quaternion orientation = path.EvaluateOrientation(time); Normal = orientation * Vector3.up; float halfWidth = path.m_Appearance.width / 2f; Vector3 originOffset = path.transform.position; RightPosition = center + orientation * Vector3.right * halfWidth; RightPosition -= originOffset; LeftPosition = center + orientation * Vector3.left * halfWidth; LeftPosition -= originOffset; }
public static int GetClosestPoint(this CinemachinePathBase path, Vector3 samplePosition) { float closestDistSqr = Mathf.Infinity; int closestPos = -1; for (int pos = (int)path.MinPos; pos < path.MaxPos; ++pos) { Vector3 worldPos = path.EvaluatePosition(pos); float distSqr = (worldPos - samplePosition).sqrMagnitude; if (distSqr < closestDistSqr) { closestDistSqr = distSqr; closestPos = pos; } } return(closestPos); }
public static void PopulateLineRenderPoints(this CinemachinePathBase path, LineRenderer renderer) { float maxStepLength = 1f / path.m_Resolution; int maxPoints = Mathf.CeilToInt(path.MaxPos / maxStepLength) + 1; Vector3[] worldPoints = new Vector3[maxPoints]; int idx = 0; for (float tdx = 0; tdx < path.MaxPos; tdx += maxStepLength) { Vector3 worldPos = path.EvaluatePosition(tdx); worldPoints[idx++] = worldPos; } renderer.positionCount = maxPoints; renderer.SetPositions(worldPoints); renderer.useWorldSpace = true; renderer.loop = path.Looped; }
// Fixed update is called in sync with physics private void FixedUpdate() { Profiler.BeginSample("Character Controller Pathing"); int startPos = Mathf.FloorToInt(currentPathPosition) - 1; if (startPos < 0) { startPos = 0; } currentPathPosition = path.FindClosestPoint(transform.position, startPos, 3, 5); Vector3 closestPoint = path.EvaluatePosition(currentPathPosition); Vector3 tangent = path.EvaluateTangent(currentPathPosition); tangent.y = 0; tangent.Normalize(); Vector3 targetDir = tangent; // read inputs float h = CrossPlatformInputManager.GetAxis("Horizontal"); float v = CrossPlatformInputManager.GetAxis("Vertical"); h = adjustedInput.Evaluate(Mathf.Abs(h)) * Mathf.Sign(h); v = adjustedInput.Evaluate(Mathf.Abs(v)) * Mathf.Sign(v); // Correction when too far away from path closestPoint.y = 0; Vector3 pos = transform.position; pos.y = 0; Vector3 dirToPath = (closestPoint - pos); dirToPath.y = 0; float pathDist = dirToPath.magnitude; pathDist -= pathDistanceTolerance; dirToPath.Normalize(); Debug.DrawLine(closestPoint, closestPoint + tangent * 3); if (pathDist > 0) { if (h < 0) { dirToPath = -dirToPath; } targetDir = Vector3.Lerp(tangent, dirToPath, pathDist * pathDistanceSteering); } Profiler.EndSample(); m_Move = targetDir * h; bool crouch = Input.GetKey(KeyCode.C); // // calculate move direction to pass to character // if (m_Cam != null) // { // // calculate camera relative direction to move: // m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized; // m_Move = v * m_CamForward + h * m_Cam.right; // } // else // { // // we use world-relative directions in the case of no main camera // m_Move = v * Vector3.forward + h * Vector3.right; // } #if !MOBILE_INPUT // walk speed multiplier if (Input.GetKey(KeyCode.LeftShift)) { m_Move *= 0.5f; } #endif // add vertical input m_Move.y = v; // pass all parameters to the character control script m_Character.Move(m_Move, crouch, m_Jump); m_Jump = false; }