Search Unity

position timing problems based on the frames per second.

Discussion in 'Scripting' started by soulreafer22, Oct 4, 2015.

  1. soulreafer22

    soulreafer22

    Joined:
    May 12, 2015
    Posts:
    15
    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:

    Code (JavaScript):
    1. private var timer = 0.0;
    2. var bobbingSpeed = 0.18;
    3. var bobbingAmount = 0.2;
    4. var midpoint = 2.0;
    5. public var swayholder: GameObject;
    6. function LateUpdate () {
    7.     waveslice = 0.0;
    8.     horizontal = Input.GetAxis("Horizontal");
    9.     vertical = Input.GetAxis("Vertical");
    10.     if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
    11.        timer = 0.0;
    12.     }
    13.     else {
    14.        waveslice = Mathf.Sin(timer);
    15.        timer = timer + bobbingSpeed;
    16.        if (timer > Mathf.PI * 2) {
    17.           timer = timer - (Mathf.PI * 2);
    18.        }
    19.     }
    20.     if (waveslice != 0) {
    21.        translateChange = waveslice * bobbingAmount;
    22.        totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
    23.        totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
    24.        translateChange = totalAxes * translateChange;
    25.        transform.localPosition.y = midpoint + translateChange;
    26.               transform.localPosition.x = midpoint + translateChange;
    27.               transform.localPosition.x = translateChange;
    28.              
    29.     swayholder.transform.localPosition.y = swayholder.transform.localPosition.y + translateChange / 2;
    30.     swayholder.transform.localPosition.x = swayholder.transform.localPosition.x + translateChange / 2 ;
    31.  
    32.              
    33.              
    34.     }
    35.     else {
    36.         transform.localPosition.y = midpoint;
    37.         swayholder.transform.localPosition.y = -0.3520508f;
    38.         swayholder.transform.localPosition.x = 1.216995f;        
    39.  
    40.     }
    41. }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599