Help I need a cube rotated I am really new to coding and hope to learn how to code from the community how would I make a cube constantly rotate on any axis X, Y, Z?
Here you go - make a script named “RotateObject.cs” and put all the code below in it. Then, add the script to your cube, and change the “RotateAmount” to what you want in the inspector.
using UnityEngine;
using System.Collections;
public class RotateObject : MonoBehaviour {
public Vector3 RotateAmount; // degrees per second to rotate in each axis. Set in inspector.
// Update is called once per frame
void Update () {
transform.Rotate(RotateAmount * Time.deltaTime);
}
}
Thanks
and where did you learn code I want to learn it really bad!
I learned it the hard way - by doing it. I recommend to take classes (of course, teaching yourself at the same time). Reason is that when you only teach yourself, you end up with holes in your knowledge that you don’t realize.
Thank you:)
i don’t run this script in my unity, Why ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateCube : MonoBehaviour {
public float spinForce;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate(0, spinForce * Time.deltaTime, 0);
}
public void ChangeSpin()
{
spinForce = -spinForce;
}
}
Add a public int to control the speed of the rotation as well!
using UnityEngine;
using System.Collections;
public class RotateObject : MonoBehaviour {
public Vector3 RotateAmount; // degrees per second to rotate in each axis. Set in inspector.
public int CutSpeed;
// Update is called once per frame
void Update () {
transform.Rotate(RotateAmount * Time.deltaTime / CutSpeed);
}
}
Call the method you created inside the Update() function:
void Update ()
{
transform.Rotate(0, spinForce * Time.deltaTime, 0);
// call the method
ChangeSpin();
}
public void ChangeSpin()
{
spinForce = -spinForce; // you might want to fix this otherwise it'd look like it's twitching
}
[Edit]
Actually your code works just fine without the method since spinForce is set to public and can be accessed in Unity Editor.