protected void moveToTarget() { //Set movement state if (currentPosition < targetPosition) { movementState = WBIMovementState.MovingForward; } else { movementState = WBIMovementState.MovingBackward; } //Set up the target position vector Vector3 currentPos = meshTransform.localPosition; meshTransform.localPosition = vecOriginalPos; meshTransform.Translate(translateAxis * targetPosition); vecTargetPos = meshTransform.localPosition; meshTransform.localPosition = currentPos; }
protected void drawMovementControls() { GUILayout.BeginHorizontal(); //If we don't have a minimum distance, then the Min button and the 0 button do the same thing, //so consolidate the GUI. if (hasMinDistance) { //Min if (GUILayout.Button(ServoGUI.minIcon, ServoGUI.buttonOptions) && meshTransform.localPosition != vecMinPosition) { targetPosition = minDistance; moveToTarget(); } } //Towards min if (GUILayout.RepeatButton(ServoGUI.backIcon, ServoGUI.buttonOptions) && meshTransform.localPosition != vecMinPosition) { targetPosition = currentPosition - velocityPerUpdate; if (targetPosition > minDistance) { moveToTarget(); if (HighLogic.LoadedSceneIsFlight) { this.part.Effect(runningEffectName, 1.0f); } } else { targetPosition = minDistance; currentPosition = minDistance; meshTransform.localPosition = vecMinPosition; movementState = WBIMovementState.Locked; status = kLocked; if (HighLogic.LoadedSceneIsFlight) { this.part.Effect(runningEffectName, 1.0f); } } } //0 if (GUILayout.Button(ServoGUI.homeIcon, ServoGUI.buttonOptions) && currentPosition != 0.0f) { targetPosition = 0f; moveToTarget(); } //Towards max if (GUILayout.RepeatButton(ServoGUI.forwardIcon, ServoGUI.buttonOptions) && meshTransform.localPosition != vecMaxPosition) { targetPosition = currentPosition + velocityPerUpdate; if (targetPosition < maxDistance) { moveToTarget(); if (HighLogic.LoadedSceneIsFlight) { this.part.Effect(runningEffectName, 1.0f); } } else { targetPosition = maxDistance; currentPosition = maxDistance; meshTransform.localPosition = vecMaxPosition; movementState = WBIMovementState.Locked; status = kLocked; if (HighLogic.LoadedSceneIsFlight) { this.part.Effect(runningEffectName, 1.0f); } } } //If we don't have a max distance, then the Max button and the 0 button do the same thing, //so consolidate the GUI. if (hasMaxDistance) { //Max if (GUILayout.Button(ServoGUI.maxIcon, ServoGUI.buttonOptions) && meshTransform.localPosition != vecMaxPosition) { targetPosition = maxDistance; moveToTarget(); } } GUILayout.EndHorizontal(); }
/// <summary> /// Tells the servo to stop moving. /// </summary> public void StopMoving() { movementState = WBIMovementState.Locked; }
public void FixedUpdate() { if (!HighLogic.LoadedSceneIsFlight && !HighLogic.LoadedSceneIsEditor) { return; } if (movementState == WBIMovementState.Locked) { return; } //Calculate movement delta and update state //Vector3.MoveTowards can also move meshTransform.localPosition but I've yet to figure out how to map the distance between meshTransform.localPosition and vecTargetPosition with minDistance and MaxDistance. float moveDelta = 0.0f; float distance = Vector3.Distance(vecMinPosition, meshTransform.localPosition); switch (movementState) { default: case WBIMovementState.Locked: break; case WBIMovementState.MovingBackward: moveDelta = -velocityPerUpdate; //Now check for target distance = Vector3.Distance(vecTargetPos, meshTransform.localPosition); if (distance <= 0.01f || currentPosition <= targetPosition) { movementState = WBIMovementState.Locked; status = kLocked; currentPosition = targetPosition; //For good measure, make sure our position is at the target meshTransform.localPosition = vecTargetPos; return; } break; case WBIMovementState.MovingForward: moveDelta = velocityPerUpdate; //Now check for target distance = Vector3.Distance(vecTargetPos, meshTransform.localPosition); if (distance <= 0.01f || currentPosition >= targetPosition) { movementState = WBIMovementState.Locked; status = kLocked; currentPosition = targetPosition; //For good measure, make sure our position is at the target meshTransform.localPosition = vecTargetPos; return; } break; } //Play the moving sound if (!string.IsNullOrEmpty(runningEffectName)) { this.part.Effect(runningEffectName, 1.0f); } //Update status status = kMoving; currentPosition += moveDelta; //Move the mesh meshTransform.Translate(translateAxis * moveDelta); }