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. Dismiss Notice

Euler angles twitches when rotating.

Discussion in 'Scripting' started by Frostbite23, Jul 13, 2015.

  1. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Hello, im upgrading and improving an old player system of mine. It works very nicely, cleaning up all the code mess I had and improving performance of it.

    Im currently adding a sway to my player so when he moves left or right, it sways in the opposite direction just slightly so it feels natural when you move around.

    In my script when I rotate the euler angles, it rotates like almost like it should but it twitches and it rotates more than the amount I set it too. Also if I try to move my mouse (This gameobject has the camera as a child object that contains a mouselook), my mouselook isint working. This is a very annoying issue, however im not sure if im using the right rotation. Either im supposed to use rotation, or euler. If anyone can help fix the twitching the issue id be thankful. Also if you would like I would like to know the difference between euler angles and rotation. Thanks.

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var cam : GameObject;
    4. var cameraa : Camera;
    5.  
    6. var swayTilt : float = 0.0f;
    7. var swaySmoothing : float = 0.0f;
    8.  
    9. private var rigid : Rigidbody;
    10.  
    11. function Start () {
    12.     rigid = gameObject.GetComponent(Rigidbody);
    13. }
    14.  
    15. function Update () {
    16.     if(rigid.velocity.x != 0.0f){
    17.         cam.transform.rotation.z = Mathf.LerpAngle(cam.transform.rotation.z, -swayTilt, Time.deltaTime * swaySmoothing);
    18.         cam.transform.rotation.z = Mathf.Clamp(cam.transform.rotation.z, -swayTilt, swayTilt);
    19.     }
    20.     else {
    21.         cam.transform.rotation.z = Mathf.LerpAngle(cam.transform.rotation.z, cam.transform.rotation.z, Time.deltaTime * swaySmoothing);
    22.         cam.transform.rotation.z = Mathf.Clamp(cam.transform.rotation.z, -swayTilt, swayTilt);
    23.     }
    24. }
    EDIT: Oops forgot to post script, and i also changed it to rotation. The twitching is still there but less prominant but the whole camera is rotated Upside down. The twitching happens once it reaches the clamp then it twitches slightly.
     
    Last edited: Jul 13, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. transform.rotation.z
    3.  
    thats the quaternion z attribute... not the euler z attribute.
     
  3. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Does not help much but thanks anyway.
    Anyone else?
     
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    Code (JavaScript):
    1. transform.rotation.eulerAngles.z = ..........
     
  5. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Tried that and it didint work.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    1. The jitter is most likely coming from being a child of a rigidbody
    2. Your code is using radian values, Im not sure this is your intention. Directly assigning values to a quat isnt going to work unless you actually understand them. Its not quite as cut and dry as your code tries to make it.
     
  7. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    1. I dont think thats likley.
    2. Radian values, heard that before. Though I havent learned about them just yet. But instead of directly assigning them your telling me to assign them as a quaternion? I think im already doing that in the script. I dont see what you mean.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you don't think its likely yet, you don't know how to fix it... ok.

    /imdonehere
     
  9. Frostbite23

    Frostbite23

    Joined:
    Mar 8, 2013
    Posts:
    458
    Im asking for help on how to fix it, otherwise I would have put [Solved] in the title.
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Your Lerp isn't correct, you're moving the starting and ending points and never reach a value of t=1. You probably want to use Mathf.MoveTowards() instead.