Automatically Rotating Objects Using Code

The point of this code is to help us better show off our models in Unity, this could be used as making them turn in display cases or just to record an overall 360 view of the model to show off. The code allows us to have the object, change directions having it spin clockwise to counterclockwise to rotating around a different point.

The point could also be used in a game sense as a way to display dropped items or to have a puzzle that incorporates spinning objects. While to code would have to be adapted to be used in a puzzle senario, I would like to see if I could change it myself and add it to my walking simulator in the future.

The code below Is what I used to adapt the speed and angle of which the object turned, I also made is so that when the user clicks on the object It changes direction. It took a fair amount of time to get the right turning speed for the object as it kept on having errors were going one way would be faster than another.

using UnityEngine;
using System.Collections;

public class Spin : MonoBehaviour {

public float speed = 5f;
public bool rotateObject = true;
public float FloatStrength;
Vector2 floatY;
void Update () {
transform.Rotate (0, 60 * Time.deltaTime, 0);

if (Input.GetMouseButtonDown(0))
if(rotateObject == true)
rotateObject = false;
rotateObject = true;

if(rotateObject == true)
transform.Rotate (new Vector3 (Time.deltaTime*0,-2,0));
floatY = transform.position;
floatY.y = (Mathf.Sin(Time.time) * FloatStrength);
transform.position = floatY;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s