super quadratic mouse-look needs polishing
Hi, I've been trying to create the seemingly ultimate mouselook for a space game.. this script spins objects 360 deg. on the x and y Axes.. in addition it will rotate 360 on the z axis.. The purpose is that I want to fly around things and go upside down or sideways when necessary but still have my local down vector mapped locally downward (upward in this case).. like I'm upside down under an asteroid moving forward.. my local down direction is actually the global up.. so when I'm flipped, the xy axis flips.. everything has been going well in that respect , but there are poles #pragma strict import System.Collections.Generic; //need for the generic list type //enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } var axes:RotationAxes = RotationAxes.MouseXAndY; var sensitivityX = 15F; var sensitivityY = 15F; var zenabled=true; var minimumX = -360F; var maximumX = 360F; var minimumY = -60F; var maximumY = 60F; var minimumZ= -89F; var maximumZ = 89F; var rotationX = 0F; var rotationY = 0F; var rotArrayX = new List.<float>(); var rotArrayY = new List.<float>(); var xflipped=false; var yflipped=false; var sp:spacepilot; var rotAverageX = 0F; var rotAverageY = 0F; var frameCounter = 20.0; var rb:Rigidbody ; var originalRotation:Quaternion; //var testa=false; //var testb=false; //var testc=false; //var testd=false; //var testf=false; var i=0.0; var onside=false; var xyadust=0.0; var Zupsideright=true; var changeY=1; var changeX=1; var testg=false; var reversepolarity=false; var ZtargetRotation=0.0; var smoothing=.5; ///////////////////////////////////////////////////////////////////////////////////////////////////// function Update () { if (Input.GetKey("a")) ZtargetRotation+=1; if (Input.GetKey("r"))//right the ship back to proper level ZtargetRotation=0; if (Input.GetKey("d")) ZtargetRotation-=1; transform.localRotation=Quaternion.Lerp(transform.localRotation,Quaternion.Euler(transform.localEulerAngles.x,transform.localEulerAngles.y,ZtargetRotation),Time.deltaTime*10/smoothing); if (ZtargetRotation>360) ZtargetRotation=0; if (ZtargetRotation<0) ZtargetRotation=360; changeY=1;//reset these values each time. changeX=1;//reset these values each time. reversepolarity=false;//reset these values each time. if (Input.GetKey("left")) rotationX-=5; if (Input.GetKey("down")) rotationY-=5; if (Input.GetKey("up"))rotationY+=5; if (Input.GetKey("right"))reversepolarity=true; //if (axes == RotationAxes.MouseXAndY){//this returns true, why ?i dont know rotAverageY = 0f;//resets rotation averages for fresh calculations each time rotAverageX = 0f; //if (rotationX>360)//makes sure the vales stay in -360 to 360 rage(they werent before i added this) //rotationX=0.0; if (rotationY>maximumY) rotationY=maximumY; //if (rotationX<-360) //rotationX=0.0; if (rotationY<minimumY ) rotationY=minimumY ; /////////////////////////////////////////////////////////// if( ( rotationY>-90 && rotationY<-270)||(rotationY>90&&rotationY<270)){//y is now 1/4 of the way right or left and entering flipped status yflipped=true; xflipped=true; } else {yflipped=false; xflipped=false; } if( ( rotationX<-90&& rotationX>-270)||(rotationX>90&&rotationX<270)){//x is now 1/4 of the way up or down and entering flipped status xflipped=true; yflipped=true; } else {yflipped=false; xflipped=false; } /////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////on either side below/////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if ((ZtargetRotation>45&&ZtargetRotation<135)||(ZtargetRotation<315 && ZtargetRotation>225))//we are on the right or left side// x and y will exchange values here i think in this zone { onside=true; if (ZtargetRotation>45&&ZtargetRotation<135){//on left side if (yflipped==true) {//if we are backwards change xrotation to negative //yflipped=false;//i think this flips y back flipX();// will make sensitivityX negative if positive and viceversa...will calculate a position change once in the end %|-1296279953_82|% if (xflipped==false){//just y is flipped flipX();// will make sensitivityX if positive flipY();// will make sensitivityY if positive } else{//x flipped too xflipped=false;//i think this flips x back as well flipY();// will make sensitivityY if positive in this case x though as the values are flipped y stayed positive here i think }//end x flipped too }//end if y flipped else {//we are just on a 45 and xy is flipped for that reason reversepolarity=true; flipY();// will make sensitivityY if positive in this case x though as the values are flipped y stayed positive here i think }//end just on side brackets }//end on right side==true else %|465685525_104|% if (ZtargetRotation<315 && ZtargetRotation>225) { //on right side==true if (yflipped==true) {//if we are backwards change xrotation to negative flipX();// will make sensitivityX negative if positive and viceversa...will calculate once in the end a positionchange flipY();// will make sensitivityY negative if positive and viceversa if (xflipped==false) {//just y is flipped flipX();// will make sensitivityX negative if positive and viceversa...will calculate once in the end a positionchange flipY();// will make sensitivityY negative if positive and viceversa }//endjust y flipped else{//x flipped too flipY();// will make sensitivityX negative if positive and viceversa...will calculate once in the end a positionchange }//endx flipped too }//end if y flipped else{ //testa=false; //we are just flipped x and y due to being on a 45 reversepolarity=true; flipX(); }//end else } //end just flipped x and y due to being on a 45 %|-1681659214_128|% %|-1280828419_129|% else {onside=false; }//end onside false ////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (ZtargetRotation>135&&ZtargetRotation<225){//if we are upsidedown change xrotation to negative//carefull z has no negatives remember its range is from 0 to 360 //z isflipped if (yflipped==true)///this will be false if on a 45 {//if we are backwards change xrotation to negative if (xflipped==false){//just y is flipped } else{//x flipped too flipX(); }}//end if y flipped else{//we are just upsidedown flipX(); flipY(); } }//end if upside down ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////// ///////// normal x rotation below ////////// ///////////////////////////////////////////////////////////////////////////////// else{//upside right //----------------------------------------------- if (ZtargetRotation<45||ZtargetRotation>315) //carefull z has no negatives remember its range is from 0 to 360 {//ship is upright start brackets Zupsideright=true; if (yflipped==true){//upside right and yflipped==true brackets //if( ( rotationY>-92 && rotationY<-268)||(rotationY>92&&rotationY<268)) flipY(); flipX();//test added if (xflipped==false){//upside right and Y flipped && x not ==true brackets }//end Y flipped && x not ==true brackets if (xflipped==true){//x is flipped too x is left right btw flipY(); flipX(); } }//end yflipped==true brackets %|635788875_182|% //x is not flipped either here so rotation is normal if (xflipped==true){//upside right and Y not flipped && x is flipped just x flipX(); flipY(); }//end xflipped==true brackets }//end ynotflipped brackets }//end shipzupright ... this value doesnt flip based on when the others do i think else {Zupsideright=false; }}//endupsideright ////////////////////////////////////////////////////////////////////////// /////////////////////////////figuring orientation above//////////////////////////////////////// /////////////////////////final calculations below//////////////////////////////////////////////////// rotationX += Input.GetAxis("Mouse X") * sensitivityX*changeX;//this will get all prior input and final movement added rotationY += Input.GetAxis("Mouse Y") * sensitivityY*changeY;//this will get all prior input and final movement added if (reversepolarity==true){ rotArrayY.Add(rotationX); rotArrayX.Add(rotationY); }else if (reversepolarity==false){ rotArrayY.Add(rotationY); rotArrayX.Add(rotationX); } if (rotArrayY.Count >= frameCounter) { rotArrayY.RemoveAt(0); } if (rotArrayX.Count >= frameCounter) { rotArrayX.RemoveAt(0); } for(var j = 0; j < rotArrayY.Count; j++) { rotAverageY += rotArrayY[j]; } for(var i = 0; i < rotArrayX.Count; i++) { rotAverageX += rotArrayX[i]; } rotAverageY /= rotArrayY.Count; rotAverageX /= rotArrayX.Count; rotAverageY = ClampAngle (rotAverageY, minimumY, maximumX); rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX); var yQuaternion:Quaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left); var xQuaternion:Quaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up); var zQuaternion:Quaternion= Quaternion.AngleAxis (transform.localEulerAngles.z, Vector3.forward); transform.localRotation = originalRotation * xQuaternion * yQuaternion * zQuaternion ; }//end update brackets ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// function Start () { rb = GetComponent.<Rigidbody>(); if (rb) rb.freezeRotation = true; originalRotation = transform.localRotation; } ///////////////////////////////////////////////////////////////////////////////////////////// function flipX(){ if (changeX==1) changeX=-1; else changeX=1; } //---------------------------------------------- function flipY(){ if (changeY==1) changeY=-1; else changeY=1; } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public static function ClampAngle (angle:float , min:float, max:float) { angle = angle % 360; if ((angle >= -360F) && (angle <= 360F)) { if (angle < -360F) { angle += 360F; } if (angle > 360F) { angle -= 360F; } } return Mathf.Clamp (angle, min, max); }
Your answer
![](https://koobas.hobune.stream/wayback/20220612080305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I have a camera problem 0 Answers
MouseLook script for rigidbody player 0 Answers
How to disable mouse look in Unity 5? 2 Answers
Apply the Camera Rotation Y to Player Gameobject Y 0 Answers
Camera can't keep up with mouse when the FPS is low 0 Answers