using UnityEngine; using CW.Common; namespace PaintIn3D { /// This component allows you to move the current Transform using editor events (e.g. UI buttons). [HelpURL(P3dCommon.HelpUrlPrefix + "P3dTranslate")] [AddComponentMenu(P3dCommon.ComponentMenuPrefix + "Translate")] public class P3dTranslate : MonoBehaviour { /// This allows you to set the coordinate space the movement will use. public Space Space { set { space = value; } get { return space; } } [SerializeField] private Space space = Space.Self; /// The movement values will be multiplied by this before use. public float Multiplier { set { multiplier = value; } get { return multiplier; } } [SerializeField] private float multiplier = 1.0f; /// If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value. /// -1 = Instantly change. /// 1 = Slowly change. /// 10 = Quickly change. public float Damping { set { damping = value; } get { return damping; } } [SerializeField] private float damping = 10.0f; /// The position will be incremented by this each second. public Vector3 PerSecond { set { perSecond = value; } get { return perSecond; } } [SerializeField] private Vector3 perSecond; [SerializeField] private Vector3 remainingDelta; /// This method allows you to translate along the X axis, with the specified value. public void TranslateX(float magnitude) { Translate(Vector3.right * magnitude); } /// This method allows you to translate along the Y axis, with the specified value. public void TranslateY(float magnitude) { Translate(Vector3.up * magnitude); } /// This method allows you to translate along the Z axis, with the specified value. public void TranslateZ(float magnitude) { Translate(Vector3.forward * magnitude); } /// This method allows you to translate along the specified vector. public void Translate(Vector3 vector) { if (Space == Space.Self) { vector = transform.TransformVector(vector); } TranslateWorld(vector); } /// This method allows you to translate along the specified vector in world space. public void TranslateWorld(Vector3 vector) { remainingDelta += vector * Multiplier; } protected virtual void Update() { var factor = CwHelper.DampenFactor(Damping, Time.deltaTime); var newDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor); transform.position += remainingDelta - newDelta; transform.Translate(perSecond * Time.deltaTime, space); remainingDelta = newDelta; } } } #if UNITY_EDITOR namespace PaintIn3D { using UnityEditor; using TARGET = P3dTranslate; [CanEditMultipleObjects] [CustomEditor(typeof(TARGET))] public class P3dTranslate_Editor : CwEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("space", "This allows you to set the coordinate space the movement will use."); Draw("multiplier", "The movement values will be multiplied by this before use."); Draw("damping", "If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.\n\n-1 = Instantly change.\n\n1 = Slowly change.\n\n10 = Quickly change."); Draw("perSecond", "The position will be incremented by this each second."); } } } #endif