- Home /
Converted MouseLook from C# to Java problems
Hey, sorry to keep posting but :/ problem and this one has struck me big time, it works perfect looking up and down but i cannot look left or right so that also stops diagonal :/ i used this code http://www.unifycommunity.com/wiki/index.php?title=MouseLookPlus and change it because they said we could in the notes but help this is my code thank you in advance to whoever fixes it
enum Axes {MouseXandY, MouseX, MouseY}
var Axis : Axes = Axes.MouseXandY;
var sensitivityX = 15.0;
var sensitivityY = 15.0;
var rotationX = 0.0;
var rotationY = 0.0;
function Update ()
{
if (Axis == Axes.MouseXandY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
}
else if (Axis == Axes.MouseX)
{
// Read the mouse input axis
rotationX -= Input.GetAxis("Mouse X") * sensitivityX;
}
else if (Axis == Axes.MouseY)
{
// Read the mouse input axis
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
}
}
please help it looks like it should work to me because they are both the same as y except has changed please help thank you
Answer by aldonaletto · Oct 07, 2011 at 12:00 AM
There's one instruction missing - just the most important, the last one in Update:
... rotationY += Input.GetAxis("Mouse Y") * sensitivityY; } // this instruction was missing: transform.localEulerAngles = Vector3(rotationY, rotationX, 0); }With this instruction, this MouseLook works perfectly in all modes. Try it and let me know if you still have problems.
thanks for trying but it gives me an error : "It is not possible to invoke an expression of type 'UnityEngine.Vector3'." but i don't know if it will help or not but i added this code based on yours - transform.localRotation = Quaternion.AngleAxis (rotationX,Vector3.up); transform.localRotation *= Quaternion.AngleAxis (rotationY,Vector3.left); now the x axis works but the y doesn't
Forgive me, I wrote the wrong thing: localEulerAngles is a property, not a function. I fixed the answer.
thank you very much :) you have been most kind, it now works :) thank you again
Answer by jpeg · Jan 14, 2012 at 01:16 AM
hate to bust in here but you forgot to limit the rotation. (please note that this is reference to anyone else who may be looking for this later on
enum Axes {MouseXandY, MouseX, MouseY} var Axis : Axes = Axes.MouseXandY;
var sensitivityX = 15.0; var sensitivityY = 15.0;
var rotationX = 0.0; var rotationY = 0.0;
var minimumY :float = -60F; var maximumY :float = 60F;
var minimumX :float = -360F; var maximumX :float = 360F;
function Update () { if (Axis == Axes.MouseXandY) { // Read the mouse input axis rotationX += Input.GetAxis("Mouse X") -sensitivityX; rotationY += Input.GetAxis("Mouse Y") -sensitivityY; } else if (Axis == Axes.MouseX) { // Read the mouse input axis rotationX -= Input.GetAxis("Mouse X") -sensitivityX; //rotationX = Mathf.Clamp (rotationX, minimumX, maximumX); } else if (Axis == Axes.MouseY) { // Read the mouse input axis rotationY += Input.GetAxis("Mouse Y") -sensitivityY; rotationY = Mathf.Clamp (rotationY, minimumY, maximumY); } transform.localEulerAngles = Vector3(rotationY, rotationX, 0); }
I annotated the limit x because I had no need for it in my code
Good point: the vertical excursion must be limited - if it reaches or passes 90 degrees the camera gets completely crazy.
Your answer
Follow this Question
Related Questions
How to change main camera and access to the other scripts?? 0 Answers
Making an object move with the camera 1 Answer
Camera movement in a 2D? (like angry birds) 1 Answer
Super mario Camera Script 0 Answers
How to change overlay with timer? 0 Answers