- Home /
Finish a rotation with cube face up.
Hi there,
I have created an grid of cubes
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:
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
Continue to rotate till it's in it's original position would be ideal.
Answer by Malcolm · Aug 31, 2011 at 03:21 AM
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.
void Update () { gameObject.transform.rotation = Quaternion.Lerp(gameObject.transform.rotation, Quaternion.identity, 12.0f * Time.deltaTime); }
Your answer
Follow this Question
Related Questions
How to make the player turn left or right only in desired places o edges and not everywhere ???? 0 Answers
[JS] transform.rotation not working 1 Answer
Anti-Gravity vehicle bounce-back help. 1 Answer
Need to change direction of transform? 2 Answers
How to rotate instantated prefab without rotating it's local directions by javascript? 1 Answer