Free Rotating Object Script for Unity3D, written in C#
Feel free to change the class name from “CoinMovement” to something more useful for you like: “RotatingObjectScript”. Just make sure whenever you create a new C# script in Unity that the name of the script and the name of the class match.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using UnityEngine; using System.Collections; public class CoinMovement : MonoBehaviour { public float myRotationSpeed = 100.0f; public bool isRotateX = false; public bool isRotateY = false; public bool isRotateZ = false; // CHANGE TO ROTATE IN OPPOSITE DIRECTION private bool positiveRotation = false; private int posOrNeg = 1; // Use this for initialization void Start () { collider.isTrigger = true; if(positiveRotation == false) { posOrNeg = -1; } } // Update is called once per frame void Update () { // Toggles X Rotation if(isRotateX) { transform.Rotate(myRotationSpeed * Time.deltaTime * posOrNeg, 0, 0);//rotates coin on X axis //Debug.Log("You are rotating on the X axis"); } // Toggles Y Rotation if(isRotateY) { transform.Rotate(0, myRotationSpeed * Time.deltaTime * posOrNeg, 0);//rotates coin on Y axis //Debug.Log("You are rotating on the Y axis"); } // Toggles Z Rotation if(isRotateZ) { transform.Rotate(0, 0, myRotationSpeed * Time.deltaTime * posOrNeg);//rotates coin on Z axis //Debug.Log("You are rotating on the Z axis"); } } } } |
Here’s a demonstration of what it does and how to use it :