fivearchers

Curve Rotate Script for Unity

Oct 22nd, 2013
1,096
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. //use an animation curve to rotate an object on its Y axis
  5. //someday will mod to include an axis selection
  6.  
  7. public class CurveRotate : MonoBehaviour {
  8.     public AnimationCurve rotateCurve;  //curve to use for the angle (usually keeping the range 0-1 on both axis)
  9.     public float offset;                //offset angle (start angle)
  10.     public float rotateAmount;          //degrees to rotate
  11.     public float rotateRate;            //speed to go thru the rotation 1=1s, 0.5=2s, 2=0.5s
  12.     public float curvePos;              //use if you want to start at a different point on the curve 0-1, so 0.5 is halfway
  13.     public bool loop=true;              //should we keep looping?
  14.     Vector3 eul;                        //this stores your rotation: Note right now it would make your angles x & z=0
  15.  
  16.     void Start () {
  17.         eul=Vector3.zero;
  18.     }
  19.    
  20.  
  21.     void Update () {
  22.         if (curvePos!=1||loop){
  23.             curvePos+=rotateRate*Time.deltaTime;
  24.             if (curvePos>1f&&loop){curvePos=curvePos-1f;}
  25.             else if (curvePos>1f&&!loop){curvePos=1f;}
  26.            
  27.             eul.y=offset+rotateAmount*rotateCurve.Evaluate(curvePos);
  28.             transform.localEulerAngles=eul;
  29.         }
  30.     }
  31. }
RAW Paste Data