position timing problems based on the frames per second.

good morning,

i dont know if the title is right but here is my problem:
i have a simple head bobbing script from the unity wiki page but i get into some trouble with the translation speed of the movement. the “bobbing” animation gets different speed based on the frames per second.

is there a way to get a fix speed no matter which frames per second the client has? here is the basic script:

private var timer = 0.0;
var bobbingSpeed = 0.18;
var bobbingAmount = 0.2;
var midpoint = 2.0;
public var swayholder: GameObject;
function LateUpdate () {
    waveslice = 0.0;
    horizontal = Input.GetAxis("Horizontal");
    vertical = Input.GetAxis("Vertical");
    if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
       timer = 0.0;
    }
    else {
       waveslice = Mathf.Sin(timer);
       timer = timer + bobbingSpeed;
       if (timer > Mathf.PI * 2) {
          timer = timer - (Mathf.PI * 2);
       }
    }
    if (waveslice != 0) {
       translateChange = waveslice * bobbingAmount;
       totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
       totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
       translateChange = totalAxes * translateChange;
       transform.localPosition.y = midpoint + translateChange;
              transform.localPosition.x = midpoint + translateChange;
              transform.localPosition.x = translateChange;
             
    swayholder.transform.localPosition.y = swayholder.transform.localPosition.y + translateChange / 2;
    swayholder.transform.localPosition.x = swayholder.transform.localPosition.x + translateChange / 2 ;

             
             
    }
    else {
        transform.localPosition.y = midpoint;
        swayholder.transform.localPosition.y = -0.3520508f;
        swayholder.transform.localPosition.x = 1.216995f;        

    }
}

Use FixedUpdate

1 Like