- Home /
Game hangs on mouse rotation script
I know this is a overly-simple problem, and I'm almost sure I'm just not attacking this correctly, but attaching this script to the camera causes the game to freeze when the appropriate mouse button is held. It remains frozen after releasing the button.
It is simply meant to rotate the camera around [0,0,0].
void Update() {
while (Input.GetButton("Fire2"))
{
transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") * Time.deltaTime);
transform.RotateAround(Vector3.zero, Vector3.right, Input.GetAxis("Mouse Y") * Time.deltaTime);
}
}
Answer by Simple · Feb 08, 2011 at 12:03 AM
Just dont use "while" construction. Update function checks every frame
thats true, but I don't want the rotation to fire unless the button is depressed. I initially tried an if statement, but it didnt fire at all.
...and I had the rotation set to too low of a value to notice when I tested it....I feel silly now. Thanks
Answer by Simple · Feb 08, 2011 at 01:22 AM
This will solve your problem:
var button_down:boolean = false; //should be global var so it will save its state through frames
function Update() { if (Input.GetButtonDown("Fire2")) button_down=true; //button_down=true until you release button if (Input.GetButtonUp("Fire2")) button_down = false;
if (button_down) { transform.RotateAround(Vector3.zero, Vector3.up, Input.GetAxis("Mouse X") Time.deltaTime); transform.RotateAround(Vector3.zero, Vector3.right, Input.GetAxis("Mouse Y") Time.deltaTime); } }
Your answer
![](https://koobas.hobune.stream/wayback/20220613103305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Camera Rotation Along Level Boundaries 1 Answer
how to move the camera using the mouse scroll in all direction? 0 Answers
Transform.LookAt - wrong vector 4 Answers
Move camera with mouse (2D) 1 Answer
Pan Orthographic Camera 0 Answers