/** * Set an end's local or world target offset based on current relative positions. * In this implementation, setting one clears the other. But technically it's no * problem to use both in tandem. */ public void RefreshTargetOffset(int end) { if (end == 0 || end == 1) { StretchyTarget t = target[end]; if (t != null) { if (t.useWorldOffset) { t.SetOffsets(Vector3.zero, t.WorldOffsetToPoint(endPoints[end])); } else { t.SetOffsets(t.LocalOffsetToPoint(endPoints[end]), Vector3.zero); } } } }
/** * Using the current arrangement of objects, * set the target.localOffset and target.worldOffset vectors for both ends. * * The ends may be swapped if needed. * * Currently the ends are both set as having either local or world offsets * on initialization. World offsets use slightly less processing, but * local offsets are the default because they are the more common case, * working properly even as the system rotates and scales in the world. */ public void RefreshTargetOffsets() { int targetCount = 0; bool[] hasTarget = new bool[2]; for (int i = 0; i < 2; i++) { if (hasTarget[i] = (target[i] != null)) { targetCount++; } } if (targetCount == 0) { return; } Vector3[] endPt = endPoints; // Get start and end points of the stretch if (targetCount == 1) { // With 1 target a reversal means the ends will be preset as: // (A) Associated with a target, but the point farther from the target // (B) Associated with no target, but the point closer to the target // // The correct solution is to swap the tether-points: // (A) Associated with a target and the point closer to the target // (B) Associated with no target and the point farther from the target int targetEnd = hasTarget[0] ? 0 : 1; // The index with a target StretchyTarget t = target[targetEnd]; // The target component // Is the end with no target actually closer to the target? if (t.WorldDistanceToPoint(endPt[1 - targetEnd]) < t.WorldDistanceToPoint(endPt[targetEnd])) { SwapTargetPoints(); endPt.Swap(); } } else // targetCount == 2 // End 1 closer to target 2? Just swap the points to get smallest offsets. { if (target[1].WorldDistanceToPoint(endPt[0]) < target[0].WorldDistanceToPoint(endPt[0])) { endPt.Swap(); } } for (int i = 0; i < 2; i++) { if (hasTarget[i]) { StretchyTarget t = target[i]; if (t.useWorldOffset) { t.SetOffsets(Vector3.zero, t.WorldOffsetToPoint(endPt[i])); } else { t.SetOffsets(t.LocalOffsetToPoint(endPt[i]), Vector3.zero); } } } } // end of RefreshTargetOffsets