using UnityEditor; using UnityEngine; public class MovingPlatform : MonoBehaviour { public float translationSpeedX = 1.0f; // Speed of translation along X-axis public float translationSpeedY = 1.0f; // Speed of translation along Y-axis public float translationSpeedZ = 1.0f; // Speed of translation along Z-axis public float rotationSpeedX = 1.0f; // Speed of rotation around X-axis public float rotationSpeedY = 1.0f; // Speed of rotation around Y-axis public float rotationSpeedZ = 1.0f; // Speed of rotation around Z-axis public float translationAmplitudeX = 1.0f; // Amplitude of translation along X-axis public float translationAmplitudeY = 1.0f; // Amplitude of translation along Y-axis public float translationAmplitudeZ = 1.0f; // Amplitude of translation along Z-axis public float rotationAmplitudeX = 30.0f; // Amplitude of rotation around X-axis public float rotationAmplitudeY = 30.0f; // Amplitude of rotation around Y-axis public float rotationAmplitudeZ = 30.0f; // Amplitude of rotation around Z-axis public bool translationX; // Enable translation along X-axis public bool translationY; // Enable translation along Y-axis public bool translationZ; // Enable translation along Z-axis public bool rotationX; // Enable rotation around X-axis public bool rotationY; // Enable rotation around Y-axis public bool rotationZ; // Enable rotation around Z-axis public bool constantrotX; // Enable nonsine translation along X-axis public bool constantrotY; // Enable nonsine translation along Y-axis public bool constantrotZ; // Enable nonsine translation along Z-axis private Vector3 _initialPosition; // Initial position of the object private Quaternion _initialRotation; // Initial rotation of the object private Transform _transform; // Cached transform private void Start() { _transform = transform; // Store the initial position and rotation of the object _initialPosition = _transform.position; _initialRotation = _transform.rotation; } private void Update() { // Calculate the new position and rotation using sine curves // Translation var translationOffsetX = translationX ? Mathf.Sin(Time.time * translationSpeedX) * translationAmplitudeX : 0.0f; var translationOffsetY = translationY ? Mathf.Sin(Time.time * translationSpeedY) * translationAmplitudeY : 0.0f; var translationOffsetZ = translationZ ? Mathf.Sin(Time.time * translationSpeedZ) * translationAmplitudeZ : 0.0f; // Rotation float rotationOffsetX; float rotationOffsetY; float rotationOffsetZ; if (constantrotX == false) rotationOffsetX = rotationX ? Mathf.Sin(Time.time * rotationSpeedX) * rotationAmplitudeX : 0.0f; else rotationOffsetX = rotationX ? Time.time * rotationSpeedX * rotationAmplitudeX : 0.0f; if (constantrotY == false) rotationOffsetY = rotationY ? Mathf.Sin(Time.time * rotationSpeedY) * rotationAmplitudeY : 0.0f; else rotationOffsetY = rotationY ? Time.time * rotationSpeedY * rotationAmplitudeY : 0.0f; if (constantrotZ == false) rotationOffsetZ = rotationZ ? Mathf.Sin(Time.time * rotationSpeedZ) * rotationAmplitudeZ : 0.0f; else rotationOffsetZ = rotationZ ? Time.time * rotationSpeedZ * rotationAmplitudeZ : 0.0f; // Calculate the rotation around the center of the object // Calculate the rotation around the center of the object Vector3 centerOffset = _transform.InverseTransformPoint(_transform.position); // Get the offset from the center of the object Quaternion quatrotationX = Quaternion.Euler(rotationOffsetX, 0, 0); Quaternion quatrotationY = Quaternion.Euler(0, rotationOffsetY, 0); Quaternion quatrotationZ = Quaternion.Euler(0, 0, rotationOffsetZ); Quaternion rotation = quatrotationX * quatrotationY * quatrotationZ; // Calculate the rotation _transform.rotation = rotation * Quaternion.LookRotation(centerOffset); // Apply rotation around the center // Update the position and rotation of the object _transform.position = _initialPosition + new Vector3(translationOffsetX, translationOffsetY, translationOffsetZ); // Update translation _transform.rotation = _initialRotation * quatrotationX * quatrotationY * quatrotationZ; // Update rotation } } #if UNITY_EDITOR // Custom editor for MovingPlatform script [CustomEditor(typeof(MovingPlatform))] public class MovingPlatformEditor : Editor { private bool _showRotationSettings = true; private bool _showTranslationSettings = true; public override void OnInspectorGUI() { // Draw the default inspector // Get the MovingPlatform script var movingPlatform = (MovingPlatform)target; // Display custom fields for translation and rotation settings // add foldout for translation settings _showTranslationSettings = EditorGUILayout.Foldout(_showTranslationSettings, "Translation Settings", true, EditorStyles.foldoutHeader); // Display translation X settings if (_showTranslationSettings) { EditorGUI.indentLevel++; // Display translation X settings movingPlatform.translationX = EditorGUILayout.Toggle("Translation X", movingPlatform.translationX); if (movingPlatform.translationX) { movingPlatform.translationSpeedX = EditorGUILayout.FloatField("Translation Speed X", movingPlatform.translationSpeedX); movingPlatform.translationAmplitudeX = EditorGUILayout.FloatField("Translation Amplitude X", movingPlatform.translationAmplitudeX); } // Display translation Y settings movingPlatform.translationY = EditorGUILayout.Toggle("Translation Y", movingPlatform.translationY); if (movingPlatform.translationY) { movingPlatform.translationSpeedY = EditorGUILayout.FloatField("Translation Speed Y", movingPlatform.translationSpeedY); movingPlatform.translationAmplitudeY = EditorGUILayout.FloatField("Translation Amplitude Y", movingPlatform.translationAmplitudeY); } // Display translation Z settings movingPlatform.translationZ = EditorGUILayout.Toggle("Translation Z", movingPlatform.translationZ); if (movingPlatform.translationZ) { movingPlatform.translationSpeedZ = EditorGUILayout.FloatField("Translation Speed Z", movingPlatform.translationSpeedZ); movingPlatform.translationAmplitudeZ = EditorGUILayout.FloatField("Translation Amplitude Z", movingPlatform.translationAmplitudeZ); } EditorGUI.indentLevel--; } // add foldout for rotation settings _showRotationSettings = EditorGUILayout.Foldout(_showRotationSettings, "Rotation Settings", true, EditorStyles.foldoutHeader); if (_showRotationSettings) { EditorGUI.indentLevel++; movingPlatform.rotationX = EditorGUILayout.Toggle("Rotation X", movingPlatform.rotationX); if (movingPlatform.rotationX) { movingPlatform.rotationSpeedX = EditorGUILayout.FloatField("Rotation Speed X", movingPlatform.rotationSpeedX); movingPlatform.constantrotX = EditorGUILayout.Toggle("Constant Rotation X", movingPlatform.constantrotX); if (!movingPlatform.constantrotX) movingPlatform.rotationAmplitudeX = EditorGUILayout.Slider("Rotation Amplitude X", movingPlatform.rotationAmplitudeX, 0.0f, 360.0f); } movingPlatform.rotationY = EditorGUILayout.Toggle("Rotation Y", movingPlatform.rotationY); if (movingPlatform.rotationY) { movingPlatform.rotationSpeedY = EditorGUILayout.FloatField("Rotation Speed Y", movingPlatform.rotationSpeedY); movingPlatform.constantrotY = EditorGUILayout.Toggle("Constant Rotation Y", movingPlatform.constantrotY); if (!movingPlatform.constantrotY) movingPlatform.rotationAmplitudeY = EditorGUILayout.Slider("Rotation Amplitude Y", movingPlatform.rotationAmplitudeY, 0.0f, 360.0f); } movingPlatform.rotationZ = EditorGUILayout.Toggle("Rotation Z", movingPlatform.rotationZ); if (movingPlatform.rotationZ) { movingPlatform.rotationSpeedZ = EditorGUILayout.FloatField("Rotation Speed Z", movingPlatform.rotationSpeedZ); movingPlatform.constantrotZ = EditorGUILayout.Toggle("Constant Rotation Z", movingPlatform.constantrotZ); if (!movingPlatform.constantrotZ) movingPlatform.rotationAmplitudeZ = EditorGUILayout.Slider("Rotation Amplitude Z", movingPlatform.rotationAmplitudeZ, 0.0f, 360.0f); } EditorGUI.indentLevel--; } } } #endif