Skip to main content
Added code.
Source Link

Edit:
full code for what I did, but I'm a fairly novice programmer and recommend you don't copy this in its entirety, it's probably quite flawed.

public float rotateSpeed;
private Vector3 turnPoint;
private float degX;
private float degZ;
private float direction;

private void Start()
{
    degX = 0; // As far as I know, it defaults to 0 without declaration, so this 2 lines aren't necessary
    degZ = 0;
}

private void FixedUpdate()
{
    Turn("Horizontal", 1, 0, Vector3.back, ref degX);
    Turn("Vertical", 0, 1, Vector3.right, ref degZ);
}

private void Turn(string button, int x, int z, Vector3 axis, ref float deg)
{
    if (Input.GetButton(button) && degX == 0 && degZ == 0) // So that it only turns in one direction at once
    {
        direction = Input.GetAxisRaw(button); // Direction needs to be stored for future use
        turnPoint = transform.position + new Vector3(0.5f * direction * x, -0.5f, 0.5f * direction * z); // Location of edge
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction); // Turn around the edge
        deg += rotateSpeed * direction; // How much it has turned so far
    }
    if (deg != 0) // If the full rotation has not yet been made, repeat
    {
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction);
        deg += rotateSpeed * direction;
        if (deg % 90 == 0) // % is a failsafe, deg should ideally be 90 or -90
        {
            deg = 0;
        }
    }
}

The way I've done it has it as a method. For now we can just ignore different scale cubes. The parameter button is either "Vertical" or "Horizontal", for now since you're working in 2 dimensions, you can just use "Horizontal" (or the accelerometer input, which I'm not sure how to use). Parameters x and z are used to locate the edge of the cube, but I feel like there's a better way of using this. Axis is just to determine direction of rotation.

Edit:
full code for what I did, but I'm a fairly novice programmer and recommend you don't copy this in its entirety, it's probably quite flawed.

public float rotateSpeed;
private Vector3 turnPoint;
private float degX;
private float degZ;
private float direction;

private void Start()
{
    degX = 0; // As far as I know, it defaults to 0 without declaration, so this 2 lines aren't necessary
    degZ = 0;
}

private void FixedUpdate()
{
    Turn("Horizontal", 1, 0, Vector3.back, ref degX);
    Turn("Vertical", 0, 1, Vector3.right, ref degZ);
}

private void Turn(string button, int x, int z, Vector3 axis, ref float deg)
{
    if (Input.GetButton(button) && degX == 0 && degZ == 0) // So that it only turns in one direction at once
    {
        direction = Input.GetAxisRaw(button); // Direction needs to be stored for future use
        turnPoint = transform.position + new Vector3(0.5f * direction * x, -0.5f, 0.5f * direction * z); // Location of edge
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction); // Turn around the edge
        deg += rotateSpeed * direction; // How much it has turned so far
    }
    if (deg != 0) // If the full rotation has not yet been made, repeat
    {
        transform.RotateAround(turnPoint, axis, rotateSpeed * direction);
        deg += rotateSpeed * direction;
        if (deg % 90 == 0) // % is a failsafe, deg should ideally be 90 or -90
        {
            deg = 0;
        }
    }
}

The way I've done it has it as a method. For now we can just ignore different scale cubes. The parameter button is either "Vertical" or "Horizontal", for now since you're working in 2 dimensions, you can just use "Horizontal" (or the accelerometer input, which I'm not sure how to use). Parameters x and z are used to locate the edge of the cube, but I feel like there's a better way of using this. Axis is just to determine direction of rotation.

Fixed code.
Source Link

I've done something similar. As isammour mentioned, use Input.GetAxis. As for why it's 0.5 up, that has to do with your RotateAround point of rotation. The easy way to solve it would be:

player.transform.RotateAround(Right.transform.position - new Vector3(0, 0.5, 0), Vector3.back, step);

or to work with other cube sizes:

player.transform.RotateAround(Right.transform.position - transform.localScale.y / 2, Vector3.back, step);

If you want to use the orientation of the phone to determine the input, I recommend you start here: https://unity3d.com/learn/tutorials/topics/mobile-touch/accelerometer-input or perhaps ask another question.

I've done something similar. As isammour mentioned, use Input.GetAxis. As for why it's 0.5 up, that has to do with your RotateAround point of rotation. The easy way to solve it would be:

player.transform.RotateAround(Right.transform.position - new Vector3(0, 0.5, 0), Vector3.back, step);

or to work with other cube sizes:

player.transform.RotateAround(Right.transform.position - transform.localScale.y, Vector3.back, step);

If you want to use the orientation of the phone to determine the input, I recommend you start here: https://unity3d.com/learn/tutorials/topics/mobile-touch/accelerometer-input or perhaps ask another question.

I've done something similar. As isammour mentioned, use Input.GetAxis. As for why it's 0.5 up, that has to do with your RotateAround point of rotation. The easy way to solve it would be:

player.transform.RotateAround(Right.transform.position - new Vector3(0, 0.5, 0), Vector3.back, step);

or to work with other cube sizes:

player.transform.RotateAround(Right.transform.position - transform.localScale.y / 2, Vector3.back, step);

If you want to use the orientation of the phone to determine the input, I recommend you start here: https://unity3d.com/learn/tutorials/topics/mobile-touch/accelerometer-input or perhaps ask another question.

Source Link

I've done something similar. As isammour mentioned, use Input.GetAxis. As for why it's 0.5 up, that has to do with your RotateAround point of rotation. The easy way to solve it would be:

player.transform.RotateAround(Right.transform.position - new Vector3(0, 0.5, 0), Vector3.back, step);

or to work with other cube sizes:

player.transform.RotateAround(Right.transform.position - transform.localScale.y, Vector3.back, step);

If you want to use the orientation of the phone to determine the input, I recommend you start here: https://unity3d.com/learn/tutorials/topics/mobile-touch/accelerometer-input or perhaps ask another question.