- Home /
Mouse Look from scratch rotating Z axis
I wrote a script to use the mouse to look around, but I'm having trouble, because it is rotating the z axis.
#pragma strict
var mouseSpeed = 0.1;
function Update()
{
//if(rigidbody)
// rigidbody.freezeRotation = true;
Screen.lockCursor = true;
var x : float = mouseSpeed * Input.GetAxis("Mouse X");
var y : float = mouseSpeed * Input.GetAxis("Mouse Y");
transform.Rotate(-y, x, 0.0);
}
You'll see that I commented out rigidbody.freezeRotation because it didn't work, nor did simply using "transform.rotation.z = 0;".
Here are some details about my program: Capsule [Rigidbody, has movement script]
Plane [As a platform] Cube [As an object to jump on] Thanks in advance!Main Camera [has mouse look script]
Answer by rutter · Oct 18, 2013 at 07:45 PM
When your object rotates around its X axis, its local Y axis changes. If you want to rotate around the world axes, [you can specify that]((http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html)):
transform.Rotate(-y, x, 0.0, Space.World);
what about:
transform.eulerAngles = new Vector3 (x, y, 0);
you might need to change x for y and y for x
AND OFC!!! :
var x : float += mouseSpeed * Input.GetAxis("$$anonymous$$ouse X");
var y : float += mouseSpeed * Input.GetAxis("$$anonymous$$ouse Y");
without that it'll rotate you only for 1°
Hey, I'm having the same problem and these solutions didn't work :/. Any updates?
This worked for me. However, I only locked the Y axis to Space.World, and kept the X axis local. If they are both locked the problem persisted. Here is what my code looked like: if(Input.GetAxis("$$anonymous$$ouse Y")!=0){
transform.Rotate(-Input.GetAxis ("$$anonymous$$ouse Y")*turnSpeed, 0, 0, Space.World);
}
if(Input.GetAxis("$$anonymous$$ouse X")!=0){
transform.Rotate(0, Input.GetAxis("$$anonymous$$ouse X")*turnSpeed, 0);
}
Answer by static_cast · Oct 28, 2013 at 09:54 PM
void Update()
{
//We wanna make sure our cursor is locked
if(Screen.lockCursor == false)
Screen.lockCursor = true;
//Weird Calculationz
yRotation -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
yRotation = Mathf.Clamp(yRotation, -80, 80);
xRotation += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
xRotation = xRotation % 360;
//Rotate camera up+down, player left+right
transform.localEulerAngles = new Vector3(yRotation, 0, 0);
player.transform.localEulerAngles = new Vector3(0, xRotation, 0);
}
This is my new code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
MouseOver Doesn't work ON trigger 3 Answers
Is it a bug? 1 Answer
Alt+Tab pointer problem 0 Answers
Mouse Movement on z-axis 0 Answers