SHARE
TWEET

Unity Pixel Movement

a guest May 8th, 2016 172 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class PixelMovement : MonoBehaviour
  2. {
  3.     protected Transform mTransform;
  4.  
  5.     protected void Awake()
  6.     {
  7.         mTransform = transform;
  8.     }
  9.  
  10.     protected virtual IEnumerator MoveTo(Vector4 values)
  11.     {
  12.         Vector3 targetPos = new Vector3(values.x,values.y,values.z);
  13.         float speed = values.w;
  14.         float currX = mTransform.localPosition.x;
  15.         float currY = mTransform.localPosition.y;
  16.         float deltaX = Math.Sign(targetPos.x - currX);
  17.         float deltaY = Math.Sign(targetPos.y - currY);
  18.         while(Vector3.SqrMagnitude(targetPos-mTransform.localPosition) > 0)
  19.         {
  20.             currX += deltaX * speed * Time.deltaTime;
  21.             currY += deltaY * speed * Time.deltaTime;
  22.             Vector3 newPos = new Vector3((int)currX,(int)currY,0f);
  23.             Vector3 currPos = mTransform.localPosition;
  24.             if(Vector3.SqrMagnitude(targetPos-currPos) < Vector3.SqrMagnitude(newPos-currPos))
  25.             {
  26.                 newPos = targetPos;
  27.             }
  28.             mTransform.localPosition = newPos;
  29.             yield return null;
  30.         }
  31.         MoveToCallback();
  32.     }
  33.  
  34.     protected virtual void MoveToCallback()
  35.     {
  36.        
  37.     }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top