Euler angles twitches when rotating.

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.

#pragma strict

var cam : GameObject;
var cameraa : Camera;

var swayTilt : float = 0.0f;
var swaySmoothing : float = 0.0f;

private var rigid : Rigidbody;

function Start () {
    rigid = gameObject.GetComponent(Rigidbody);
}

function Update () {
    if(rigid.velocity.x != 0.0f){
        cam.transform.rotation.z = Mathf.LerpAngle(cam.transform.rotation.z, -swayTilt, Time.deltaTime * swaySmoothing);
        cam.transform.rotation.z = Mathf.Clamp(cam.transform.rotation.z, -swayTilt, swayTilt);
    }
    else {
        cam.transform.rotation.z = Mathf.LerpAngle(cam.transform.rotation.z, cam.transform.rotation.z, Time.deltaTime * swaySmoothing);
        cam.transform.rotation.z = Mathf.Clamp(cam.transform.rotation.z, -swayTilt, swayTilt);
    }
}

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.

transform.rotation.z

thats the quaternion z attribute… not the euler z attribute.

Does not help much but thanks anyway.
Anyone else?

transform.rotation.eulerAngles.z = ..........

Tried that and it didint work.

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

you don’t think its likely yet, you don’t know how to fix it… ok.

/imdonehere

Im asking for help on how to fix it, otherwise I would have put [Solved] in the title.

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.