I have tried a few head bobbing scripts out on unity and they work ok but I want the bobbing speed faster when the player holds the shift key and also some footsteps sounds that will play according to the speed of the bobbing? How can this be done? Please reply if you can help. Thanks! Here’s the basic headbobbing code I got from the Unify community Author: (Mr. Animator)
private var timer = 0.0;
var bobbingSpeed = 0.18;
var bobbingAmount = 0.2;
var midpoint = 2.0;
function Update () {
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;
}
}
I understand your frustration but disagree with your sentiment. The frustration is similar to the frustration I feel each time I try to do something significant in Blender or Modo. I’m of the opinion that any significant application is a combination of designer/artistic talent combined with programming talent. Significant applications are not constructed cookie cutter fashion from acquired scripts any more than significant applications are constructed out of free clipart. And companies make choices on how they invest their resources. Unity has a thriving business, and as a programmer, I am happy with their choices. As a designer, you also have choices about the tools you use to develop your apps. GamesStudio is still in business (A8 engine now), and their prices seemed reasonable.
As for head bobbing. Here is a modification to the script you posted above. I made bobbing speed commensurate with the axis values (i.e. the harder you lean on the joysticks, the faster the bobbing). And the script will now play a sound for each step.
#pragma strict
@script RequireComponent(AudioSource)
private var timer = 0.0;
var bobbingSpeed = 20.0;
var bobbingAmount = 0.2;
var midpoint = 2.0;
private var bPos = true;
function Update () {
var waveslice = 0.0;
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
var bobbingFactor = Mathf.Sqrt(horizontal * horizontal + vertical * vertical);
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
timer = 0.0;
}
else {
waveslice = Mathf.Sin(timer);
var footfall = Mathf.Cos(timer);
if (footfall < 0 && bPos)
audio.Play();
bPos = footfall >= 0;
timer = timer + bobbingSpeed * bobbingFactor * Time.deltaTime;
if (timer > Mathf.PI * 2) {
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0) {
var translateChange = waveslice * bobbingAmount;
var 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;
}
}