Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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:
    6,204
    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
    Ryuuguu, UnityUsage100, RGEzu and 2 others 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:
    6,204
    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.
     
    RGEzu, konsic and reezun like this.
  5. yusufhan34

    yusufhan34

    Joined:
    Dec 24, 2016
    Posts:
    5
    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:
    5
    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. }
     
  8. Deleted User

    Deleted User

    Guest

    Call the method you created inside the Update() function:

    Code (CSharp):
    1. void Update ()
    2. {
    3.     transform.Rotate(0, spinForce * Time.deltaTime, 0);
    4.     // call the method
    5.     ChangeSpin();
    6. }
    7.  
    8. public void ChangeSpin()
    9. {
    10.     spinForce = -spinForce;  // you might want to fix this otherwise it'd look like it's twitching
    11. }
    [Edit]
    Actually your code works just fine without the method since spinForce is set to public and can be accessed in Unity Editor.
     
    Last edited by a moderator: Aug 4, 2020
    ginaraince likes this.