private void FixedUpdate() { if (NetGame.isClient || ReplayRecorder.isPlaying) { if (joint != null) { Object.Destroy(joint); } return; } if (joint == null) { CreateJoint(); } float num = parameters.maxSpeed * input.value; if (currentSpeed < num) { if (parameters.accelerationTime > 0f) { currentSpeed += parameters.maxSpeed * (Time.fixedDeltaTime / parameters.accelerationTime); currentSpeed = Mathf.Min(currentSpeed, parameters.maxSpeed); } else { currentSpeed = num; } } if (currentSpeed > num) { if (parameters.decelerationTime > 0f) { currentSpeed -= parameters.maxSpeed * (Time.fixedDeltaTime / parameters.decelerationTime); currentSpeed = Mathf.Max(currentSpeed, 0f - parameters.maxSpeed); } else { currentSpeed = num; } } if (num == 0f && Mathf.Abs(currentSpeed) < 0.05f) { currentSpeed = 0f; } Vector3 a = base.transform.forward * currentSpeed; Vector3 projected = Vector3.zero; Vector3 position = base.transform.position; current = Vector3.Lerp(current, position, (position - current).magnitude); if (Rail.Project(current + a * Time.fixedDeltaTime, ref projected, ref currentRail, ref currentSegment)) { current = projected; joint.connectedAnchor = current; } else if (!seendebugstring1 && showDebug) { Debug.Log(base.name + "No track?"); seendebugstring1 = true; } }
public static bool Next(ref Rail currentRail, ref int currentIndex, ref bool fwd) { RailEnd railEnd = null; if (fwd) { if (currentIndex < currentRail.points.Length - 2) { currentIndex++; return(true); } railEnd = currentRail.end.connectedTo; } else { if (currentIndex > 0) { currentIndex--; return(true); } railEnd = currentRail.start.connectedTo; } if (railEnd == null) { currentRail = null; currentIndex = -1; return(false); } currentRail = railEnd.rail; if (currentRail.start == railEnd) { fwd = true; currentIndex = 0; } else { fwd = false; currentIndex = currentRail.points.Length - 1; } return(true); }
public void ProjectSegment(Vector3 worldPos, int index, ref float bestDistSqr, ref Vector3 bestProjected, ref Rail bestRail, ref int bestIndex) { Vector3 vector = Math3d.ProjectPointOnLineSegment(points[index], points[(index + 1) % points.Length], worldPos); float sqrMagnitude = (vector - worldPos).sqrMagnitude; if (sqrMagnitude < bestDistSqr) { bestDistSqr = sqrMagnitude; bestProjected = vector; bestRail = this; bestIndex = index; } }
public static bool Project(Vector3 worldPos, ref Vector3 projected, ref Rail currentRail, ref int currentIndex) { Debug.DrawRay(worldPos, Vector3.up, Color.gray); float bestDistSqr = float.MaxValue; Vector3 bestProjected = Vector3.zero; Rail bestRail = null; int bestIndex = 0; if (currentRail != null) { currentRail.ProjectSegment(worldPos, currentIndex, ref bestDistSqr, ref bestProjected, ref bestRail, ref bestIndex); Rail currentRail2 = currentRail; int currentIndex2 = currentIndex; bool fwd = true; for (int i = 0; i < 5; i++) { if (!Next(ref currentRail2, ref currentIndex2, ref fwd)) { break; } currentRail2.ProjectSegment(worldPos, currentIndex2, ref bestDistSqr, ref bestProjected, ref bestRail, ref bestIndex); } currentRail2 = currentRail; currentIndex2 = currentIndex; fwd = false; for (int j = 0; j < 5; j++) { if (!Next(ref currentRail2, ref currentIndex2, ref fwd)) { break; } currentRail2.ProjectSegment(worldPos, currentIndex2, ref bestDistSqr, ref bestProjected, ref bestRail, ref bestIndex); } if (bestDistSqr > 1f) { currentRail = null; } } if (currentRail == null) { for (int k = 0; k < all.Count; k++) { for (int l = 0; l < all[k].points.Length - 1; l++) { all[k].ProjectSegment(worldPos, l, ref bestDistSqr, ref bestProjected, ref bestRail, ref bestIndex); } } } if (bestDistSqr <= 1f) { currentRail = bestRail; currentIndex = bestIndex; projected = bestProjected; Debug.DrawRay(projected, Vector3.up, Color.green); return(true); } currentRail = null; currentIndex = -1; projected = Vector3.zero; return(false); }