I want to edit this script, but I don’t really understand it. What I want is to play a footstep sound when the headbob reaches its lowest point.
Can someone help me understand this script. Maybe put comments or if you know how I will add in the sound at the right point. Please teach a man to fish!
public class HeadBob : MonoBehaviour {
private float timer = 0.0f;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;
public float count = 0;
float waveslice;
float horizontal;
float vertical;
Vector3 cSharpConversion;
void Update () {
waveslice = 0.0f;
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
cSharpConversion = transform.localPosition;
if(transform.localPosition.y < 1.9f)
transform.parent.GetComponent<FootSteps>().MoveOnConcrete();
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;
cSharpConversion.y = midpoint + translateChange;
}
else {
cSharpConversion.y = midpoint;
}
transform.localPosition = cSharpConversion;
}
}