Search Unity

Rigidbody MoveRotation on Global Axis

Discussion in 'Scripting' started by 5vStudios, May 7, 2016.

  1. 5vStudios

    5vStudios

    Joined:
    May 9, 2015
    Posts:
    106
    Hey Guys,
    So I'm currently developing a First Person Simulation system and everything works perfectly,
    However though I have a small problem with the MouseLook script which is attached to a child camera of the PlayerGO, it controls the rotation of both the rootGO (ie: Player on Y-Axis) and the camera itself obviously.

    Code (csharp):
    1.         float Sensitivity = 5f;
    2.         float xRotation, yRotation;
    3.         float CurrentXRotation, CurrentYRotation;
    4.         float xRotationV, yRotationV;
    5.         public float SmoothDamp = 0.1f;
    6.  
    7.         void FixedUpdate()
    8.         {
    9.             xRotation -= Input.GetAxisRaw ("Mouse Y") * Sensitivity;
    10.             yRotation += Input.GetAxisRaw ("Mouse X") * Sensitivity;
    11.  
    12.             xRotation = Mathf.Clamp (xRotation, -80, 80);
    13.  
    14.             CurrentXRotation = Mathf.SmoothDamp (CurrentXRotation, xRotation, ref xRotationV, SmoothDamp);
    15.             CurrentYRotation = Mathf.SmoothDamp (CurrentYRotation, yRotation, ref yRotationV, SmoothDamp);
    16.  
    17.             RotateBody ();
    18.  
    19.             transform.rotation = Quaternion.Euler (CurrentXRotation, CurrentYRotation, 0);
    20.         }
    21.  
    22.         float currYRot, rf;
    23.         void RotateBody()
    24.         {
    25.             Rigidbody rb = PlayerController.instance.rb;
    26.  
    27.             float yRot = Input.GetAxisRaw ("Mouse X");
    28.             currYRot = Mathf.SmoothDamp (currYRot, yRot, ref rf, SmoothDamp);
    29.  
    30.             Vector3 rot = new Vector3 (0f, currYRot, 0f) * Sensitivity;
    31.  
    32.             rb.MoveRotation(rb.rotation * Quaternion.Euler(rot));
    33.         }
    so the quirk i'm experiencing is that I am unable to figure out how to modify Rigidbody MoveRotation in the RotateBody() function to rotate on the global axis rather than local axis which it currently rotates on.

    Any ideas, suggestions or advice ?