Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Rotating cube script?

Discussion in 'Editor & General Support' started by iWoundPwn, Feb 17, 2013.

  1. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    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?
     
  2. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    4,567
    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.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RotateObject : MonoBehaviour {
    6.  
    7.     public Vector3 RotateAmount;  // degrees per second to rotate in each axis. Set in inspector.
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.         transform.Rotate(RotateAmount * Time.deltaTime);
    12.     }
    13. }
    14.  
     
    Last edited: Feb 18, 2013
    StreetBat and reezun like this.
  3. iWoundPwn

    iWoundPwn

    Joined:
    Feb 16, 2013
    Posts:
    212
    Thanks :) and where did you learn code I want to learn it really bad!
     
    reezun likes this.
  4. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    4,567
    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.
     
    konsic and reezun like this.
  5. yusufhan34

    yusufhan34

    Joined:
    Dec 24, 2016
    Posts:
    1
    Thank you:)
     
  6. dayatdevgame

    dayatdevgame

    Joined:
    Apr 24, 2019
    Posts:
    1
    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;
    }
    }
     
  7. NaturedOne

    NaturedOne

    Joined:
    Jan 10, 2013
    Posts:
    3
    Add a public int to control the speed of the rotation as well!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class RotateObject : MonoBehaviour {
    4.     public Vector3 RotateAmount;  // degrees per second to rotate in each axis. Set in inspector.
    5.     public int CutSpeed;
    6.  
    7.     // Update is called once per frame
    8.     void Update () {
    9.         transform.Rotate(RotateAmount * Time.deltaTime / CutSpeed);
    10.     }
    11. }