public void FixedUpdate() { if (!switching) { return; } var percent = (Time.time - switchStartTime) / switchSpeed; if (switchSpeed == 0 || percent >= 1) { if (endSwitching) { track.ConnectTo(positions[desiredPosition]); } else { track.SnapTogether(positions[desiredPosition], true, false); } switching = false; } else { if (endSwitching) { track.TrackAbsoluteEnd = SimpleTransform.Lerp( lastPosition, positions[desiredPosition].TrackAbsoluteStart, percent ); } else { track.TrackAbsoluteStart = SimpleTransform.Lerp( lastPosition, positions[desiredPosition].TrackAbsoluteEnd, percent ); } } }
/** Finds the nearest track in the given direction, then snaps to it as requested. */ public void FindAndSnap(Track track, bool snapMe, bool snapEnd) { Track other = null; SimpleTransform myEdge; if (snapEnd) { other = track.NextTrack; myEdge = track.TrackAbsoluteEnd; } else { other = track.PrevTrack; myEdge = track.TrackAbsoluteStart; } if (!other) { //let's go looking for things since we're not currently linked float nearestPos = float.PositiveInfinity; foreach (Track sceneTrack in Object.FindObjectsOfType(typeof(Track))) { //skip ourself if (sceneTrack == track) { continue; } if (!sceneTrack.enabled) { continue; } //if they are connected to us unidirectionally, prefer completing that link instead of picking a new track if ((snapEnd && sceneTrack.PrevTrack == track) || (!snapEnd && sceneTrack.NextTrack == track)) { other = sceneTrack; break; } //skip items that are already linked to something on the end we would connect to if ((snapEnd && sceneTrack.PrevTrack) || (!snapEnd && sceneTrack.NextTrack)) { continue; } SimpleTransform stp; if (snapEnd) { stp = sceneTrack.TrackAbsoluteStart; } else { stp = sceneTrack.TrackAbsoluteEnd; } if (Vector3.Distance(myEdge.position, stp.position) < nearestPos) { other = sceneTrack; nearestPos = Vector3.Distance(myEdge.position, stp.position); } } } if (!other) { Debug.Log("Unable to find another track to snap to."); return; } //mark down undo for both itemst var objects = GetObjectsInvolvedWithTrack(track).Union(GetObjectsInvolvedWithTrack(other)).ToArray(); Undo.RecordObjects(objects, "Snap Track"); track.SnapTogether(other, snapMe, snapEnd); }