- Home /
How can I create headbobbing with footstep sounds and running?
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;
}
}
Before someone on this list gives you an answer, are sure this is the code you want to use? In particular this code only works if you are going to use "Horizontal" and "Vertical" for your inputs. If you are using something else (arrow keys for example or asdw), then this code will not work. Does this code work for you now? In addition, this code doesn't appear to be FPS independent. The bobbing will run at different speeds on different platforms (fixable).
Hi robertbu, Well thats the thing theres nowhere else that I can find a free or useful bobbing script. Plus this from the Unify community so its tyhe most common and trustworthy (supposedly) one that Ive been able to find and Ive been searching for over a month now. And yes this code does work for me now (except just for the fact that I dont know how to make the headbobbing speed up when running or how to sync a footstep sound with the camera's bob movements) and its not restricted to jus the X and Y axis inputs cause when i push forward (w) the camera bobs (thats generally how a camera bob script works, last time I checked, you need to move forward).
What still baffles me is how the hell does Unity not give you these necessary basic scripts, I used a middleware called 3D Gamestudio (A6 engine) almost 10 years ago and the support and scripts (not to mention ease of coding) in it. Even back then was 10 times better than Unity. Theres no real useful free stuff, all the free stuff is half-assed at best and all the useful stuff is like $60. What the hell this is supposed to be middleware, Im a designer not a coder. Im sorry for being so agitated but this is really unacceptable, I dont know how Unity gets away with this appaling lack of service and support.
yea I mean you cant design without coding. Im not sure what you mean by ease of coding. The C# language is supported and is along with C++ basically a hugely standard language with years of support and a library of functions. There are a HUGE A$$anonymous$$OUNT of built in scripts in unity. want physics or animations or shaders or terrain it's all there.
I mean what exactly do you mean your a designer? You write stuff in notebooks? Well that's nice and I can draw a nice house but if I want the house to exist I learn engineering and make blueprints and if I want to I can get a program to make blueprints but it's quite silly of me to get a program to make blueprints. Expect it to have hundreds of generic blueprints I can combine together to make my house and then go "see what i did there, i'm an engineer".
Unity is middleware but it isn't Wal-mart middleware. It's like the difference between "designing" a room where you say I want a cabinet and you just look at cabinets until you end up with one you like or designing one by going to lowes, buying wood, putting it together and staining it.
Game$$anonymous$$aker is a generic list of stuff you can combine together to make something and perhaps that is what you want. Unity is what you use PRECISELY WHEN nothing out there is what you want. If your looking for a function to make an object move up and down at a variable speed sure that exists google it. If you want it to move up and down and take into account other inputs and store that speed and use it to modify other objects well no that doesn't exist or at least not in general.
Generic head bobbing functions could be thrown in. But it would almost certainly not support variable speed based on input and variable sound files attached with the option to vary the speed of the sound file based on input from the bob speed. It'd have to be extended out to gain that functionality.
People who can extend out that functionality would find the intial coding of lerping an object up and down to be so trivial they wouldnt need a built in function for 5 lines of code. (which is what your generic function would look like). People who can't know nothing about program$$anonymous$$g and should get a friend who does or use a program that doesnt require that knowledge.
Just to help you out though. Heres your function (it doesn't exist in general because its crazy easy to write) Attach to head gameobject. This is a trivially easy generic head bobbing function. That will copy/paste
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : $$anonymous$$onoBehaviour {
float TopExtent;
float BotExtent;
float Speed;
bool BobbingUp;
Vector3 Change;
void Start(){
//height defines how high your head bobs up IN $$anonymous$$ETERS
TopExtent = .1f;
BotExtent = transform.localPosition.y;
// speed is in m/s
Speed = .1f;
Change = Vector3.zero;
BobbingUp = true;
}
void Update(){
Change = new Vector3(0,Speed * Time.deltaTime,0);
if(BobbingUp)
{
if(transform.localPosition.y < TopExtent)
transform.localPosition += Change;
else
BobbingUp = false;
}
else{
if(transform.localPosition.y > BotExtent)
transform.localPosition -= Change;
else
BobbingUp = true;
}
}
}
Answer by robertbu · Mar 11, 2013 at 05:21 AM
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;
}
}
Your answer
Follow this Question
Related Questions
Footsteps Problem 2 Answers
Increase speed of footsteps when sprinting? 2 Answers
Controlling Animation Headbob with Walking Speed? 1 Answer
Trail Renderer - limit trail lenght? 0 Answers
Changing zoom speed? 1 Answer