Finish a rotation with cube face up.

Hi there,

I have created an grid of cubes

alt text

And I have the following script that rotates individual cubes when the mouse is pressed down and dragged across:

var rotationSpeed = 10.0;
var lerpSpeed = 2.0;

private var speed = new Vector3();
private var avgSpeed = new Vector3();
private var dragging = false;
private var targetSpeedX = new Vector3();

function OnMouseOver() 
{
    dragging = true;
}

function OnMouseExit() 
{
    dragging = false;
}


function Update () 
{

    if (Input.GetMouseButton(0) && dragging) {
        speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
        avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
    } else {
        if (dragging) {
            speed = avgSpeed;
              dragging = false;
          
        }
        var i = Time.deltaTime * lerpSpeed;
        speed = Vector3.Lerp( speed, Vector3.zero, i);   
    }

  transform.Rotate(Vector3.forward, speed.x * rotationSpeed, Space.World);
}

However, once the cubes have finished rotating the end up something like this:

alt text

So my question is, whats the best method to use, so that when the cube finishes rotating it returns to it’s original position, i.e face up.

Thanks

As in it like snapping to position? or it will continue to rotate till it is in it's original position

1 Answer

1

You can set the Transform.rotation of each cube to Quaternion.identity. If you need to animate this you may need to use Quaternion.Lerp.