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
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 :))
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.