I have a headbobber script that I found online, and it is working well, but unfortunately it isn’t set up to be framerate independent. I use Time.deltaTime all the time, but am not sure how I would apply it in this context, could anybody help me out? Thanks.
using UnityEngine;
using System.Collections;
public class Headbobber : MonoBehaviour {
private float timer = 0.0f;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;
void Update () {
float waveslice = 0.0f;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
timer = 0.0f;
} else {
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2) {
timer = timer - (Mathf.PI * 2);
}
}
Vector3 v3T = transform.localPosition;
if (waveslice != 0) {
float translateChange = waveslice * bobbingAmount;
float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp (totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
v3T.y = midpoint + translateChange;
} else {
v3T.y = midpoint;
}
transform.localPosition = v3T;
}
}