Skip to content

Instantly share code, notes, and snippets.

@GuilleUCM
Created February 13, 2015 08:24
Show Gist options
  • Save GuilleUCM/9b35b36e539032cdf943 to your computer and use it in GitHub Desktop.
Save GuilleUCM/9b35b36e539032cdf943 to your computer and use it in GitHub Desktop.
Unity:Animation:Spin rotation
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