Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Weird snappy rotation

Discussion in 'Scripting' started by xXBlueFire, Oct 26, 2020.

  1. xXBlueFire

    xXBlueFire

    Joined:
    Mar 18, 2020
    Posts:
    3
    Hello guys,

    I'm pretty new to coding and working with unity so please have mercy with me ^^'
    But I have fun experementing a bit with Unity and working on a dumb childhood dream^^

    But here my problem now, I'm currently working on a 3rd person character controller where I use
    spherical coordinates to rotate the camera around the player. Probably not the best way to do so, but I wanted to try it like that. Now it's workling almost fine except that my character is "jiggeling" if it is rotated. I think, that the lookAt method isn't compatible with my code because if I use it in the LateUpdate function the "jiggeling" is just a bit, but if I use it in the beginning of the Update function where my camera position is calculated it starts "jiggeling" super hard.
    Also it becomes worse if I select the playermodel in the scene.
    Here a video: YouTube (in the description are timestamps)
    Does anybody know what the problem is?
    Also I'm using a rigedbody for my character

    Here my code:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         //Camera.transform.LookAt(LookAt);
    4.  
    5.         x = r * Mathf.Cos(phi) * Mathf.Sin(theta);
    6.         z = r * Mathf.Sin(phi) * Mathf.Sin(theta);
    7.         y = r * Mathf.Cos(theta);
    8.         newPosition = new Vector3(x, y, z);
    9.  
    10.         Camera.position = transform.position + newPosition + startPosition;
    11.  
    12.         //Adjust r
    13.         mouse_scroll = controls.Camera.ScrollWheel.ReadValue<Vector2>();
    14.         r += mouse_scroll.y * Time.deltaTime * scrollingSpeed;
    15.         r = Mathf.Clamp(r, max_r, min_r);
    16.  
    17.         //Adjust phi and theta
    18.         mouse_position_raw = controls.Camera.MouseDelta.ReadValue<Vector2>();
    19.         if (phi >= 2 * pi || phi < -2 * pi)     //reset phi
    20.         {
    21.             phi = 0f;
    22.         }
    23.         theta += rotationSpeedTheta * Time.deltaTime * mouse_position_raw.y;
    24.         theta = Mathf.Clamp(theta, theta_limitation, pi - theta_limitation);
    25.         float rotation = rotationSpeedPhi * Time.deltaTime * mouse_position_raw.x;
    26.  
    27.         if (sidewaysMovement != 0 || straightMovement != 0)
    28.         {
    29.             if(freeLook == true)
    30.             {
    31.                 transform.localRotation = Quaternion.Euler(0, -phi * Mathf.Rad2Deg, 0);
    32.             }
    33.             phi -= rotation;
    34.             transform.Rotate(0, rotation * Mathf.Rad2Deg, 0);
    35.         }
    36.         else
    37.         {
    38.             freeLook = true;
    39.             phi -= rotation;
    40.         }
    41.     }
    42.     private void FixedUpdate()
    43.     {
    44.         speed = 6.0f;
    45.         if (straightMovement < 0.0f)
    46.         {
    47.             speed = 2.0f;
    48.         }
    49.         if (sidewaysMovement != 0.0f)
    50.         {
    51.             speed = 4.0f;
    52.         }
    53.         if (straightMovement < 0.0f && sidewaysMovement != 0)
    54.         {
    55.             speed = 2.0f;
    56.         }
    57.  
    58.         straightMovement = controls.Player.StraightMovement.ReadValue<float>();
    59.         sidewaysMovement = controls.Player.SidewaysMovement.ReadValue<float>();
    60.  
    61.         moveDirection.x = straightMovement * speed * Time.deltaTime;    //WS
    62.         moveDirection.z = sidewaysMovement * speed * Time.deltaTime;    //AD
    63.  
    64.         transform.Translate(moveDirection.x, 0, moveDirection.z);
    65.         //playerBody.AddForce(moveDirection, ForceMode.VelocityChange);
    66.     }
    67.     private void LateUpdate()
    68.     {
    69.         Camera.transform.LookAt(LookAt);
    70.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Traditionally the camera should be moved in FixedUpdate if its position is dictated by a Physics object.

    Have you tried just combining the above blocks of code into FixedUpdate()? Or at least doing the camera position setting and LookAt-ing in FixedUpdate()?
     
  3. xXBlueFire

    xXBlueFire

    Joined:
    Mar 18, 2020
    Posts:
    3
    Hey, yes I tried it and it doesn't change :/