Mirace
February 7, 2011, 5:20pm
1
I have HeadBobbing script in my Maincamera, and it works normaly. But if i Build Run game At Fullscreen (not tested windowed) bobbing goes wrong speed. Copy that code, paste it to the javascript and attach that to the main camera on First Person Controller prefab. Any help is welcome.
-Purri
private var timer = 0.0;
var bobbingSpeed = 0.18;
var bobbingAmount = 0.2;
var midpoint = 2.0;
function Update () {
if(Input.GetKeyDown(KeyCode.LeftShift)){bobbingSpeed = 0.02;}
if(Input.GetKeyUp(KeyCode.LeftShift)){bobbingSpeed = 0.01;}
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;
}
else {
transform.localPosition.y = midpoint;
}
}
your code is missing the * Time.deltaTime in the line where it applies the translation change.
without this the code is FPS dependent and as the standalone runs at different speeds it will also result in a different bobbing behaviour
there are a few threads on this, just search for FPS independent coding
Mirace
February 7, 2011, 5:38pm
3
So translateChange = waveslice * bobbingAmount * Time.deltaTime; should be correct?
Mirace
February 7, 2011, 6:05pm
4
Solved here solution
private var timer = 0.0;
var bobbingSpeed = 0.18;
var bobbingAmount = 0.2;
var midpoint = 2.0;
function Update () {
if(Input.GetKeyDown(KeyCode.LeftShift)){bobbingSpeed = 10;}
if(Input.GetKeyUp(KeyCode.LeftShift)){bobbingSpeed = 6;}
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 += bobbingSpeed * Time.deltaTime;
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;
}
else {
transform.localPosition.y = midpoint;
}
}