so i am making a FPS head bob script and really i had it in javaScript i’m just converting it to C#. But i get a reference error dealing with a variable that accesses the localPosition of the object the script is attached to. This is my ENTIRE 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;
}
}
}
here is the error i am getting
error CS0120: An object reference is required to access non-static member `UnityEngine.Transform.localPosition’
the script is on the player’s fps camera.
any help is appreciated
there is no error message now but the script isnt working. It has no effect on the player's camera. But thanks for the help.
– anon21109274