How to alter rotations using the mouse?

I’m trying to change the roation of a cylinder object using the mouse input, but the Unity Editor crashes when ever the function is carried out. In addition, I need to set up each rotation axis is changed when the mouse is used:

X axis = Forward and backwards mouse input
Y axis = Mouse wheel
Z axis = Left and right mouse input

Here’s the code so far:

function Release(){
transform.parent = null;
transform.rotation = Quaternion.Euler(0, 0, 0);
rigidbody.isKinematic = false;
rigidbody.useGravity = false;
collider.isTrigger = false;
carrying = false;
Rotate();
}

function Rotate(){
placing = true;
while (placing){
transform.localRotation = Quaternion.Euler(0,0,Input.mousePosition.z);
}
if(Input.GetMouseButtonDown(0)){
	placing = false;
	}
}

Any help is appreciated.

Im not sure whats causing the crash or if your Rotate() is working as you intended, try this and see if it clears it up: (yeah its C# but i’m sure you can translate it to java :))

void Rotate()
{
    bool placing = true;
    while (placing)
    {
        rotationX += Input.GetAxis("Mouse X") * sensitivityX;
        rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
        clampRotation();
        transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
        transform.localRotation = Quaternion.AngleAxis(rotationY, Vector3.left);

        if (Input.GetMouseButtonDown(0))
        {
            placing = false;
        }
    }
}

void clampRotation()
{
    if (rotationX < -360)
        rotationX += 360;
    else if (rotationX > 360)
        rotationX -= 360;

    if (rotationY < -360)
        rotationY += 360;
    else if (rotationY > 360)
        rotationY -= 360;
}

Your while loop will block Unity on the frame it is called on, never giving Unity a chance to draw again or to update the input handling.

You could either:

1. Get rid of the loop, move the logic to e.g. the Update() function, rotating a bit every frame until the mouse button is down:

if (placing) {
    // ... rotate
    placing = !Input.GetMouseButtonDown(0);
}

2. Leave Rotate() mostly as is, but yield every loop iteration:

function Rotate() {
    while (true) {
        if (Input.GetMouseButtonDown(0)) {
            return;
        }
        // ... rotate
        yield;
    }
}

You were blocking Unity with a while loop, like @gfr and @syclamoth said - you must place a yield instruction inside it to let Unity free until the next update cycle.

Another thing: Input.mousePosition is in screen pixels, and its z component is always zero, thus the object would never rotate anyway. As @chemicalvamp suggested, you must accumulate the mouse displacement informed by Input.GetAxis(“Mouse X”) or other axes in a variable and use it to set the rotation angle. With these fixes, your script would become:

function Release(){
  transform.parent = null;
  transform.rotation = Quaternion.identity; // it's faster and easier then Euler(0,0,0)
  rigidbody.isKinematic = false;
  rigidbody.useGravity = false;
  collider.isTrigger = false;
  carrying = false;
  Rotate();
}

var sensitivity: float = 60;

function Rotate(){
  if (placing) return; // avoid multiple Rotate running at the same time
  placing = true;
  var angles = transform.localEulerAngles; // get the current orientation
  while (placing){
    angles.x = (angles.x + sensitivity * Input.GetAxis("Mouse Y") * Time.deltaTime) % 360;
    angles.y = (angles.y + sensitivity * Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime) % 360;
    angles.z = (angles.z + sensitivity * Input.GetAxis("Mouse X") * Time.deltaTime) % 360;
    transform.localEulerAngles = angles;
    yield; // let Unity free until next frame
    if(Input.GetMouseButtonDown(0)){
      placing = false;
    }
  }
}

NOTE: You may have to define the “Mouse ScrollWheel” axis in your Input Manager, and perhaps change the sensitivity for this particular axis.