ArenMook

SmoothTransform

Dec 22nd, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Smooth Transform script can be used to smooth out abrupt changes in the transform's position, rotation and velocity.
  5. /// Its ideal use is attached to a renderer root of a moving object that gets repositioned as part of a network update.
  6. /// This script should never be attached to the root object. It must always go on a child object containing the renderers.
  7. /// </summary>
  8.  
  9. public class SmoothTransform : MonoBehaviour
  10. {
  11.     public float lerpTime = 0.5f;
  12.  
  13.     Transform mParent;
  14.     Transform mTrans;
  15.     Rigidbody mRb;
  16.     Vector3 mFromWorldPos;
  17.     Vector3 mLocalPos;
  18.     Quaternion mFromWorldRot;
  19.     Quaternion mLocalRot;
  20.     Vector3 mFromVel;
  21.     Vector3 mFromAngVel;
  22.     float mStart = 0f;
  23.  
  24.     public Transform trans { get { if (mTrans == null) Cache(); return mTrans; } }
  25.  
  26.     void Cache ()
  27.     {
  28.         mTrans = transform;
  29.         mParent = mTrans.parent;
  30.         mRb = GetComponentInParent<Rigidbody>();
  31.         mLocalPos = mTrans.localPosition;
  32.         mLocalRot = mTrans.localRotation;
  33.     }
  34.  
  35.     static float EaseInOut (float val)
  36.     {
  37.         const float pi2 = Mathf.PI * 2f;
  38.         return val - Mathf.Sin(val * pi2) / pi2;
  39.     }
  40.  
  41.     void LateUpdate ()
  42.     {
  43.         if (lerpTime == 0f || mTrans == null) { enabled = false; return; }
  44.  
  45.         var time = Time.time;
  46.         var delta = time - mStart;
  47.         var factor = delta / lerpTime;
  48.  
  49.         if (factor < 1f)
  50.         {
  51.             factor = EaseInOut(factor);
  52.             var estimatedPos = mFromWorldPos + mFromVel * delta;
  53.             var estimatedRot = Quaternion.Euler(mFromAngVel * delta) * mFromWorldRot;
  54.  
  55.             var pos = Vector3.Lerp(estimatedPos, mParent.TransformPoint(mLocalPos), factor);
  56.             var rot = Quaternion.Slerp(estimatedRot, mParent.rotation * mLocalRot, factor);
  57.  
  58.             mTrans.position = pos;
  59.             mTrans.rotation = rot;
  60.         }
  61.         else Finish();
  62.     }
  63.  
  64.     /// <summary>
  65.     /// Prepare to animiate. Caches all needed values.
  66.     /// </summary>
  67.  
  68.     public void Init ()
  69.     {
  70.         if (mTrans == null) Cache();
  71.  
  72.         mStart = Time.time;
  73.         mFromWorldPos = mTrans.position;
  74.         mFromWorldRot = mTrans.rotation;
  75.  
  76.         if (mRb != null)
  77.         {
  78.             mFromVel = mRb.velocity;
  79.             mFromAngVel = mRb.angularVelocity * Mathf.Rad2Deg;
  80.         }
  81.         else
  82.         {
  83.             mFromVel = Vector3.zero;
  84.             mFromAngVel = Vector3.zero;
  85.         }
  86.     }
  87.  
  88.     /// <summary>
  89.     /// Activate the smooth transform transition.
  90.     /// </summary>
  91.  
  92.     public void Activate ()
  93.     {
  94.         enabled = true;
  95.         LateUpdate();
  96.     }
  97.  
  98.     public void Finish ()
  99.     {
  100.         if (mTrans == null) Cache();
  101.         mTrans.position = mParent.TransformPoint(mLocalPos);
  102.         mTrans.rotation = mParent.rotation * mLocalRot;
  103.         enabled = false;
  104.     }
  105.  
  106.     /// <summary>
  107.     /// Given the specified world position, transform it to the value adjusted by the smoothing operation.
  108.     /// Use it if you need to have some renderer positions to be adjusted that can't be a part of the smoothing hierarchy, for example.
  109.     /// NOTE: It assumes that the SmoothTransform has no starting rotation present. If rotation is required, it should be on the child object.
  110.     /// </summary>
  111.  
  112.     public Vector3 TransformPoint (Vector3 worldPos)
  113.     {
  114.         if (enabled)
  115.         {
  116.             if (mTrans == null) Cache();
  117.             worldPos = mParent.InverseTransformPoint(worldPos);
  118.             worldPos = mTrans.TransformPoint(worldPos - mLocalPos);
  119.         }
  120.         return worldPos;
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment