- Home /
how do i smooth out this movement c#
This script is attached to a empty game object and a gun attached to it, on forward movement its very jittery. I am thinking if I can add the line of code to a smoothDamp function it will fix the issue problem I am having is converting a vector3 to a float.
I also tried slerp but not sure what to set the second Quaternion too.
here is the code
public GameObject cameraObject;
public float smoothSpeedRotation = 0.5f;
public float holdHeight = -0.42f;
public float holdSide = 0.42f;
//public float holdForward = 0.1f;
float targetXRotation;
float targetYRotation;
float targetXRotationVelocity;
float targetYRotationVelocity;
// Update is called once per frame
void Update () {
//effecting the transform of the gun.
transform.position = cameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0)* new Vector3(holdSide,holdHeight,0));
//smothly effect the guns rotation
targetXRotation = Mathf.SmoothDamp(targetXRotation,cameraObject.GetComponent<MouseLookScript>().rotationX,ref targetXRotationVelocity,smoothSpeedRotation);
targetYRotation = Mathf.SmoothDamp(targetYRotation,cameraObject.GetComponent<MouseLookScript>().rotationY,ref targetYRotationVelocity,smoothSpeedRotation);
//physically move the gun
transform.rotation = Quaternion.Euler(targetXRotation,targetYRotation,0);
}
awesome thank you that never even crossed my $$anonymous$$d I forget about LateUpdate If you would either make your answer the answer or change post to answer so I may make it as answered thank you.
Answer by robertbu · May 26, 2013 at 05:01 PM
When dealing with moving object in relationship to a moving camera, jitter problems can usually be fixed by moving the code from Update() to LateUpdate().
I've converted it to an answer. Note I did not answer the separate question about a smoothed rotation. I did not understand the desired behavior (i.e. why you are pulling out only the x and y rotation of the camera). If you can explain, I can perhaps give you code for this as well, or you can open it in a new question.
the reason for x and y rotation is spinning around those axes did not want to turn around the z axes the smoothdamp function works great and I rotate the way I am wanting I do thank you for offering more help.