This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Move using A Curve for Unity3D

By: fivearchers on Oct 22nd, 2013  |  syntax: C#  |  size: 1.02 KB  |  views: 380  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data