Created
February 13, 2015 08:24
-
-
Save GuilleUCM/9b35b36e539032cdf943 to your computer and use it in GitHub Desktop.
Unity:Animation:Spin rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// Spins or rotates a gameObject around Y-axis (up) | |
/// </summary> | |
public class Spin : MonoBehaviour { | |
/// <summary> | |
/// Number of complete rotations per second | |
/// </summary> | |
public float loopsPerSecond=1; | |
/// <summary> | |
/// Total time the gameobject will spin | |
/// </summary> | |
public float animationTime=1; | |
/// <summary> | |
/// Whether the rotation is clockwise or anti-clockwise | |
/// </summary> | |
public bool clockwise = true; | |
private float time; | |
private float angle; | |
private float cwmod; | |
/// <summary> | |
/// Initialization when enabled | |
/// </summary> | |
void OnEnable () { | |
time = animationTime; | |
angle = 360*loopsPerSecond; | |
cwmod = clockwise? 1.0f :-1.0f; | |
} | |
void FixedUpdate () { | |
time-=Time.fixedDeltaTime; | |
if (time>0) { | |
transform.Rotate(Vector3.up, cwmod*angle*Time.fixedDeltaTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment