- Home /
Multiple rotate with mouse
If mouse moves upper/lower. Camera will rotate upper and lower between limits. For example: local rotation between Vector3(+45, 0, 0) and Vector3(-40, 0, 0) If mouse moves right/left. Parent object of camera will rotate right/left around Y. For example local rotation Vector3(0, 173, 0) or anything else. In there no limit. Also always mouse will stay at center. I am using c#. you can answer in javascript too, not much problem.
(Edit: Forget to say turrent have a maksimum rotation speed for example: 60 degree per sec so turrent cant rotate too fast also idk how to adapt it)
Answer by SuperProgrammer · Jul 22, 2012 at 10:02 AM
Thanks for help
I added final coding:
First i added a variable stores current X rotation because X rotation of camera changes between 320(-40) to 45, 5f is sensitivity, -40 and 45 are limits. Req -Input.GetAxis("Mouse Y") (negative) for if mouse moves upper, came will too else doing opposite
private float CameraXRotation = 0f;
void Update () {
CameraXRotation = Mathf.Clamp(CameraXRotation - Input.GetAxis("Mouse Y") * 5f, -40f, 45f);
transform.localEulerAngles = new Vector3(CameraXRotation, 0, 0);
}
similar code for turret (not req a variable for store Y rotation)
transform.parent.localEulerAngles = new Vector3(0, Mathf.Min(transform.parent.localEulerAngles.y + Input.GetAxis("Mouse X") * 5f, Time.deltaTime * MaxTurretRotationSpeed), 0);
And for lock mouse at center of screen + hidden cursor
Screen.lockCursor = true;
for unlock set it to false.
Answer by ScroodgeM · Jul 21, 2012 at 08:20 PM
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetAxis.html
just read
Input.GetAxis("Mouse Y")in camera's control script and apply to locally declared property of float. then clamp it to -40...45 and apply to rotation as
transform.rotation = Quaternion.Euler(new Vector3(val, 0f, 0f));
separately from previous script just read
Input.GetAxis("Mouse X")in turret's control script and make
transform.Rotate(0, Mathf.Min(val * mouseSensitivity, Time.deltaTime * rotationMaxSpeed), 0);