Skip to main content
editted format
Source Link
Valkea
  • 2.8k
  • 3
  • 18
  • 22
using UnityEngine;

using System.Collections; using UnityEngine.UI;

public class RotateCube : MonoBehaviour { public Text txt; public float speed; public Vector3 targetEul, myEul; public bool canRotate, doLeft, doRight; int i ;

using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{
    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    void Update ()
    {
        //check input when possible
        if (canRotate) 
            checkInput ();
        if (doLeft) 
            checkLeft ();
        if (doRight) 
            checkRight ();
    }

    //rotaterottate left
    float chrono;
    void checkLeft ()
    {
        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }
    }

    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;            
        }
    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {
            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90,  
        //Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            targetEul = transform.eulerAngles;
            targetEul.x += 90;          
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;
        }
    
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }   
}

}

using UnityEngine;

using System.Collections; using UnityEngine.UI;

public class RotateCube : MonoBehaviour { public Text txt; public float speed; public Vector3 targetEul, myEul; public bool canRotate, doLeft, doRight; int i ;

void Update ()
{
    //check input when possible
    if (canRotate) 
        checkInput ();
    if (doLeft) 
        checkLeft ();
    if (doRight) 
        checkRight ();
}

//rotate left
float chrono;
void checkLeft ()
{
    if (chrono > 0) {
        chrono -= speed * Time.deltaTime;
        transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
        myEul.x += speed * Time.deltaTime;
    } else {
        transform.rotation = Quaternion.Euler (targetEul);
        //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
        canRotate = true;
        doLeft = false;
    }
}

//rotate right
float chronoR;

void checkRight ()
{
    if (chronoR > 0) {
        chronoR -= speed * Time.deltaTime;
        transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
        myEul.z += speed * Time.deltaTime;
    } else {
        transform.rotation = Quaternion.Euler (targetEul);
        //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
        canRotate = true;
        doRight = false;            
    }
}

//check input
Quaternion targetQuat;

void checkInput ()
{
    if (Input.GetKeyDown (KeyCode.LeftArrow)) {
        //targetQuat = transform.rotation * (Quaternion.AngleAxis (90,  
        //Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
        targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
        targetEul = transform.eulerAngles;
        targetEul.x += 90;          
        chrono = 90;
        canRotate = false;
        doLeft = true;
        i++;
    }
    
    if (Input.GetKeyDown (KeyCode.RightArrow)) {
        targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
        targetEul = transform.eulerAngles;
        targetEul.z += 90;
        chronoR = 90;
        canRotate = false;
        doRight = true;
        i++;
    }
    txt.text = "" + i;
}   

}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{
    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    void Update ()
    {
        //check input when possible
        if (canRotate) 
            checkInput ();
        if (doLeft) 
            checkLeft ();
        if (doRight) 
            checkRight ();
    }

    //rottate left
    float chrono;
    void checkLeft ()
    {
        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }
    }

    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;            
        }
    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {
            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90,Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            targetEul = transform.eulerAngles;
            targetEul.x += 90;          
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;
        }
    
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }   
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{

    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    

using System.Collections; using UnityEngine.UI;

public class RotateCube : MonoBehaviour { public Text txt; public float speed; public Vector3 targetEul, myEul; public bool canRotate, doLeft, doRight; int i ;

void Update ()
    {
 
        //check input when possible
        if (canRotate) {
            checkInput ();
        }

    
        if (doLeft) {
            checkLeft ();
        }


        if (doRight) {
            checkRight ();
        }

    }

    //rotate left
    float chrono;
    void checkLeft ()
    {


 
        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;

 
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }

 
    }
 

    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;
             
        }
 
    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {
 
            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90,  
        //Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            //
            targetEul = transform.eulerAngles;
            targetEul.x += 90;
            //
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;





 
        }
        
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            //
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            //
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }
    
}

}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{

    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    void Update ()
    {
 
        //check input when possible
        if (canRotate) {
            checkInput ();
        }

    
        if (doLeft) {
            checkLeft ();
        }


        if (doRight) {
            checkRight ();
        }

    }

    //rotate left
    float chrono;
    void checkLeft ()
    {


 
        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;

 
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }

 
    }
 

    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;
             
        }
 
    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {
 
            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90, Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            //
            targetEul = transform.eulerAngles;
            targetEul.x += 90;
            //
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;





 
        }
        
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            //
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            //
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }
    
}
using UnityEngine;

using System.Collections; using UnityEngine.UI;

public class RotateCube : MonoBehaviour { public Text txt; public float speed; public Vector3 targetEul, myEul; public bool canRotate, doLeft, doRight; int i ;

void Update ()
{
    //check input when possible
    if (canRotate) 
        checkInput ();
    if (doLeft) 
        checkLeft ();
    if (doRight) 
        checkRight ();
}

//rotate left
float chrono;
void checkLeft ()
{
    if (chrono > 0) {
        chrono -= speed * Time.deltaTime;
        transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
        myEul.x += speed * Time.deltaTime;
    } else {
        transform.rotation = Quaternion.Euler (targetEul);
        //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
        canRotate = true;
        doLeft = false;
    }
}

//rotate right
float chronoR;

void checkRight ()
{
    if (chronoR > 0) {
        chronoR -= speed * Time.deltaTime;
        transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
        myEul.z += speed * Time.deltaTime;
    } else {
        transform.rotation = Quaternion.Euler (targetEul);
        //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
        canRotate = true;
        doRight = false;            
    }
}

//check input
Quaternion targetQuat;

void checkInput ()
{
    if (Input.GetKeyDown (KeyCode.LeftArrow)) {
        //targetQuat = transform.rotation * (Quaternion.AngleAxis (90,  
        //Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
        targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
        targetEul = transform.eulerAngles;
        targetEul.x += 90;          
        chrono = 90;
        canRotate = false;
        doLeft = true;
        i++;
    }
    
    if (Input.GetKeyDown (KeyCode.RightArrow)) {
        targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
        targetEul = transform.eulerAngles;
        targetEul.z += 90;
        chronoR = 90;
        canRotate = false;
        doRight = true;
        i++;
    }
    txt.text = "" + i;
}   

}

added 2478 characters in body
Source Link
alaslipknot
  • 472
  • 6
  • 19

EDIT here is the entire Class am using in this example :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{

    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    void Update ()
    {

        //check input when possible
        if (canRotate) {
            checkInput ();
        }

    
        if (doLeft) {
            checkLeft ();
        }


        if (doRight) {
            checkRight ();
        }

    }

    //rotate left
    float chrono;
    void checkLeft ()
    {



        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;


        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }


    }


    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;
            
        }

    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {

            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90, Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            //
            targetEul = transform.eulerAngles;
            targetEul.x += 90;
            //
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;






        }
        
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            //
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            //
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }
    
}

EDIT here is the entire Class am using in this example :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class RotateCube : MonoBehaviour
{

    public Text txt;
    public  float speed;
    public Vector3  targetEul, myEul;
    public bool canRotate, doLeft, doRight;
    int i ;

    void Update ()
    {

        //check input when possible
        if (canRotate) {
            checkInput ();
        }

    
        if (doLeft) {
            checkLeft ();
        }


        if (doRight) {
            checkRight ();
        }

    }

    //rotate left
    float chrono;
    void checkLeft ()
    {



        if (chrono > 0) {
            chrono -= speed * Time.deltaTime;
            transform.Rotate (speed * Time.deltaTime, 0, 0, Space.World);
            myEul.x += speed * Time.deltaTime;


        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp(transform.rotation,targetQuat,100);
            canRotate = true;
            doLeft = false;
        }


    }


    //rotate right
    float chronoR;

    void checkRight ()
    {
        if (chronoR > 0) {
            chronoR -= speed * Time.deltaTime;
            transform.Rotate (0, 0, -speed * Time.deltaTime, Space.World);
            myEul.z += speed * Time.deltaTime;
        } else {
            transform.rotation = Quaternion.Euler (targetEul);
            //transform.rotation = Quaternion.Slerp (transform.rotation, targetQuat, 100);
            canRotate = true;
            doRight = false;
            
        }

    }

    //check input
    Quaternion targetQuat;

    void checkInput ()
    {
        if (Input.GetKeyDown (KeyCode.LeftArrow)) {

            //targetQuat = transform.rotation * (Quaternion.AngleAxis (90, Vector3.left));Quaternion.Euler (transform.rotation.eulerAngles.x + 90, 0, 0);
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x + 90, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);//Quaternion.AngleAxis(90,Vector3.right);
            //
            targetEul = transform.eulerAngles;
            targetEul.x += 90;
            //
            chrono = 90;
            canRotate = false;
            doLeft = true;
            i++;






        }
        
        if (Input.GetKeyDown (KeyCode.RightArrow)) {
            targetQuat = Quaternion.Euler (transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z + 90);
            //
            targetEul = transform.eulerAngles;
            targetEul.z += 90;
            //
            chronoR = 90;
            canRotate = false;
            doRight = true;
            i++;
        }
        txt.text = "" + i;
    }
    
}
Source Link
alaslipknot
  • 472
  • 6
  • 19
Loading