I have a cylinder, that I want to spin. Normally I would have no problem here with this code :
void Update () {
wheel.Rotate(Vector3.right * rotateVelocity * Time.deltaTime);
}
Unfortunately, this doesn’t work because the center of the wheel is offset far away. Centering it brings up some problems. So I want to manually set the center by putting an empty gameObject in the middle of the cylinder. I need the cylinder to rotate around the that empty gameObject. How do I set the object that I want the cylinder to rotate around?
What I have done in the past is that I set the cylinder as a child of the empty game object(once i have placed the game object where I want it) I then attach this Javascript to the cylinder, you will have to edit your x,y,z variables in the inspector to get it to rotate how you want:
var xSpeed : float = 1;
var ySpeed : float = 1;
var zSpeed : float = 1;
var manual : boolean = false;
function Update ()
{
if( !manual )
{
transform.RotateAround( transform.position, Vector3.right, ySpeed * Time.deltaTime );
transform.RotateAround( transform.position, Vector3.up, xSpeed * Time.deltaTime );
transform.RotateAround( transform.position, Vector3.forward, zSpeed * Time.deltaTime );
}
else
{
if( Input.GetAxis("Horizontal") != 0 )
{
transform.RotateAround( transform.position, Vector3.up, Input.GetAxis("Horizontal")*xSpeed * Time.deltaTime );
}
if( Input.GetAxis("Vertical") != 0 )
{
transform.RotateAround( transform.position, Vector3.right, Input.GetAxis("Vertical")*ySpeed * Time.deltaTime );
}
}
}
Nevermind, I figured it out. I just used RotateAround, and set the position using a variable Transform. Here’s the code for anyone who wants it :
using UnityEngine;
using System.Collections;
public class SpinningWheel : MonoBehaviour {
public float rotateVelocity;
public Transform pivot;
private Transform wheel;
void Awake () {
wheel = transform;
}
void Update () {
wheel.RotateAround(pivot.position, Vector3.right, rotateVelocity * Time.deltaTime);
}
}