fivearchers

Move using A Curve for Unity3D

Oct 22nd, 2013
861
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3. //script to apply movement over time based on a curve
  4. //Quentin Preik - 2012/10/22
  5. //@fivearchers
  6. public class CurveMove : MonoBehaviour {
  7.     public AnimationCurve moveCurve;    //Curve to use - use 1 second long, usually 1 unity high
  8.     public Vector3 movement;            //Amount of movement applied by this curve - so 5,25,0, would
  9.                                         //move the object 5 right, 25 up when the curve is at 1
  10.     public float moveRate;              //How fast to go thru the curve:  1=1s, 2=0.5s, 0.5=2s etc
  11.     public float curvePos;              //Use this as an offset - so 0.5 would start halfway thru the curve
  12.     Vector3 localstart;
  13.     public bool loop=true;              //Whether to loop or only run once
  14.    
  15.     void Start () {
  16.         localstart=transform.localPosition;
  17.     }
  18.    
  19.    
  20.     void Update () {
  21.         if (curvePos!=1||loop){
  22.             curvePos+=moveRate*Time.deltaTime;
  23.             if (curvePos>1f&&loop){curvePos=curvePos-1f;}
  24.             else if (curvePos>1f&&!loop){curvePos=1f;}
  25.             transform.localPosition=localstart+movement*moveCurve.Evaluate(curvePos);
  26.         }
  27.     }
  28. }
RAW Paste Data