Skip to content

Instantly share code, notes, and snippets.

@pringithub
Created May 1, 2019 15:06
Show Gist options
  • Save pringithub/4b118c74d86a6bf14c9d573621c3e269 to your computer and use it in GitHub Desktop.
Save pringithub/4b118c74d86a6bf14c9d573621c3e269 to your computer and use it in GitHub Desktop.
Unity C# code to move and resize a cube using your hands
var leftDiff = (leftIndex.transform.position - leftThumb.transform.position).magnitude;
var rightDiff = (rightIndex.transform.position - rightThumb.transform.position).magnitude;
if (leftDiff < PINCH_DISTANCE && rightDiff < PINCH_DISTANCE)
{
var leftX = leftIndex.transform.position.x;
var leftY = leftIndex.transform.position.y;
var leftZ = leftIndex.transform.position.z;
var rightX = rightIndex.transform.position.x;
var rightY = rightIndex.transform.position.y;
var rightZ = rightIndex.transform.position.z;
var x_mid = (leftX + rightX) / 2;
var y_mid = (leftY + rightY) / 2;
var z_mid = (leftZ + rightZ) / 2;
var x_scale = Abs(leftX - rightX) * SCALE_PCT;
var y_scale = Abs(leftY - rightY) * SCALE_PCT;
var z_scale = Abs(leftZ - rightZ) * SCALE_PCT;
this.transform.position = new Vector3(x_mid, y_mid, z_mid);
this.transform.localScale = new Vector3(x_scale, y_scale, z_scale);
this.transform.rotation = Quaternion.identity;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment