So ive been wanting to make a “head bob” script for my fps character. I found one example of one on a website. It was in javaScript so i converted it to C# the best i could(im making the game for the flash platform so i dont want to deal with javaScript). I put it on my fps player’s camera. However it has NO EFFECT on the camera. Can anyone tell me why this is? Here is my script:

``````using UnityEngine;
using System.Collections;

public class FPSHeadBob : MonoBehaviour {

private float timer = 0.0f;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;

void Update () {
float lpY = transform.localPosition.y;
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);
}
}
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;
lpY = midpoint + translateChange;
}
else
{
lpY = midpoint;
}
}
}
``````

Any help is appreciated.

Hi!

IpY is no pointer to the transform y or something. The calculated value is never written back to the actual transform.

The last lines in Update must be something like:

``````Vector3 newPos = transform.localPosition;
newPos.y += IpY;
transform.localPosition = newPos;
``````

or a bit more compressed

``````transform.localPosition = transform.localPosition + transform.up * IpY;
``````

But that may not lead to a result also, because the actual movement happens somewhere else. You may need to change the Controller script directly and apply it in the calculation there, otherwise you never know which script is calculated first.

Good luck