- Home /
How do I make a camera bob when I walk?
I am making a first person shooter, and I would really like the camera to kinda bob up and down when you walk. how would I do that? Thanks in advance.
Answer by Eric5h5 · Dec 04, 2010 at 07:58 PM
Try this script: http://www.unifycommunity.com/wiki/index.php?title=Headbobber
Thank you so much. it worked perfectly. (for people who will need this also, make sure you set the code to the main camera, and set the mid point to 1 and uncheck mesh renderer for your first person controller.
ill receive this:
- Finished compile Library/ScriptAssemblies/Assembly-UnityScript.dll
Assets/Scripts/CameraBobing.js(9,5): BCE0005: $$anonymous$$ identifier: 'waveslice'.
(Filename: Assets/Scripts/CameraBobing.js Line: 9)
Assets/Scripts/CameraBobing.js(10,5): BCE0005: $$anonymous$$ identifier: 'horizontal'.
(Filename: Assets/Scripts/CameraBobing.js Line: 10)
Assets/Scripts/CameraBobing.js(11,5): BCE0005: $$anonymous$$ identifier: 'vertical'.
(Filename: Assets/Scripts/CameraBobing.js Line: 11)
Assets/Scripts/CameraBobing.js(12,19): BCE0005: $$anonymous$$ identifier: 'horizontal'.
(Filename: Assets/Scripts/CameraBobing.js Line: 12)
Assets/Scripts/CameraBobing.js(12,49): BCE0005: $$anonymous$$ identifier: 'vertical'.
(Filename: Assets/Scripts/CameraBobing.js Line: 12)
Assets/Scripts/CameraBobing.js(16,8): BCE0005: $$anonymous$$ identifier: 'waveslice'.
(Filename: Assets/Scripts/CameraBobing.js Line: 16)
Error: Analytics Event: 5(Compiler*BCE0005*$$anonymous$$ identifier: 'waveslice'.)(1): skipped because it was sent more than once in 0.10 seconds
Assets/Scripts/CameraBobing.js(22,9): BCE0005: $$anonymous$$ identifier: 'waveslice'.
(Filename: Assets/Scripts/CameraBobing.js Line: 22)
Assets/Scripts/CameraBobing.js(23,8): BCE0005: $$anonymous$$ identifier: 'translateChange'.
(Filename: Assets/Scripts/CameraBobing.js Line: 23)
Assets/Scripts/CameraBobing.js(24,8): BCE0005: $$anonymous$$ identifier: 'totalAxes'.
(Filename: Assets/Scripts/CameraBobing.js Line: 24)
Error: Analytics Event: 5(Compiler*BCE0005*$$anonymous$$ identifier: 'totalAxes'.)(1): skipped because it was sent more than once in 0.10 seconds
Assets/Scripts/CameraBobing.js(25,8): BCE0005: $$anonymous$$ identifier: 'totalAxes'.
(Filename: Assets/Scripts/CameraBobing.js Line: 25)
Assets/Scripts/CameraBobing.js(26,8): BCE0005: $$anonymous$$ identifier: 'translateChange'.
(Filename: Assets/Scripts/CameraBobing.js Line: 26)
Error: Analytics Event: 5(Compiler*BCE0005*$$anonymous$$ identifier: 'translateChange'.)(1): skipped because it was sent more than once in 0.10 seconds
Assets/Scripts/CameraBobing.js(27,47): BCE0005: $$anonymous$$ identifier: 'translateChange'.
(Filename: Assets/Scripts/CameraBobing.js Line: 27)
Works, but I can't jump... What should I do? Also I set the midpoint to 2 because 1 wont let me move and glitches the game. $$anonymous$$y character height is 4 so maybe thats why.
the site has been taken down. Can this script be found anywhere else, (has the site been archived on waybackmachine?) or is it gone forever?
Answer by friketrike · Nov 12, 2012 at 04:15 PM
Originally based on this one, I made another script in C# which requires CharacterController and CharacterMotor, just drop it into the child camera and you should be set to go. I hope this is of use to someone. Cheers, using UnityEngine; using System.Collections;
public class HeadBobbing : MonoBehaviour {
// drop into the main character's camera
// editor-accessible vars
public float bobbingFreq = 1.8f; // in Hz, TODO make it dependent on speed
private float bobbingFreqCached;
public float bobbingRatio = 0.08f; // as a factor of character height
// non editor-accessible
private float phase = Mathf.PI;
private CharacterMotor characterMotor; // we need to see if the character is walking or jumping (TODO maybe running and sliding)
private CharacterController characterController;
private float height; //we'll need this for bobbing as a function of height
private float bobbingAmount;
private float heightDependency;
private float currentBobbing; // keep track of y axis modification
private float bobbingDelta; // how much has bobbing changed in the last frame
// Keep a mask for the displacement states where isWalking is xxx1 isStepping xx1x and isStopping x1xx
private int stateMask = 0;
private const int isWalking = 1;
// this next variable can be useful for triggering stepSounds, see getter below
private const int isStepping = 2;
private const int isStopping = 4;
// need to remember where camera was before we began toying with it.
// NOTE this wont work if anything else is moving the camera!
private Vector3 OriginalCameraLocalPosition;
void Start (){
characterMotor = GetComponent<CharacterMotor>();
characterController = GetComponent<CharacterController>();
height = characterController.height;
OriginalCameraLocalPosition = Camera.main.transform.localPosition;
heightDependency = transform.localScale.y * height / 2;
currentBobbing = 0.0f;
bobbingFreqCached = bobbingFreq;
}
void Update (){
bobbingAmount = bobbingRatio * heightDependency;
if ( (characterMotor.inputMoveDirection != Vector3.zero) && characterMotor.IsGrounded() ) {
UpdatePhaseAndBobbingDelta();
stateMask = stateMask | isWalking; // isWalking = true;
stateMask = stateMask & ~isStopping; // isStopping = false;
}
else {
// bobbingDelta = 0.0f; // see note in our first branch
if( (stateMask & isWalking) > 0 ){ // if we got here, we're stopping
stateMask = stateMask | isStopping; // isStopping = true
UpdatePhaseAndBobbingDelta( (phase > 0) ? 1 : -1 ); // if our phase is less than 0, rewind it
if ( (stateMask & isStepping) > 0 ) {// we're back down
Camera.main.transform.localPosition = OriginalCameraLocalPosition; // avoid cumulative errors
stateMask = stateMask & ~isWalking;
phase = Mathf.PI; // ready to start again
}
}
else {
bobbingDelta = 0.0f;
stateMask = 0; // we're not doing anything
}
}
Camera.main.transform.Translate(Vector3.up * bobbingDelta, Space.World);
}
// by default we add phase but it might be shorter to rewind it when stopping
private void UpdatePhaseAndBobbingDelta (){
UpdatePhaseAndBobbingDelta( 1.0f );
}
private void UpdatePhaseAndBobbingDelta ( float direction ){
float twoPi = Mathf.PI * 2;
// bobbing happens here, add one to Sin to avoid going under the terrain
float previousBobbing = currentBobbing;
currentBobbing = (Mathf.Cos(phase) + 1.0f) * bobbingAmount;
bobbingDelta = currentBobbing - previousBobbing;
// update phase in a frame-independent fashion
phase = phase + (direction * (twoPi * Time.deltaTime * bobbingFreq));
// wrap phase
if (Mathf.Abs(phase) > Mathf.PI) {
phase = phase - (direction * twoPi);
stateMask = stateMask | isStepping;
}
else
stateMask = stateMask & ~isStepping;
}
public bool getIsStepping(){
return ( (isStepping & stateMask) > 0 );
}
}
Thanks so much I have been looking everywhere online for a c# bobbing script. Do I have permission to use this?? Thanks
Oh boy, I saw this a bit late. For sure, you can use it, I put it up for sharing. Damn shame too, since I had an improved version with running and crouching and I lost it in a defunct hard drive. Cheers
Is there any particular place we need to put this or will any camera script do? Also, it's giving me $$anonymous$$ identifiers for 'bobbingSpeed', 'bobbingAmount', and 'midpoint'. How can this be fixed?
Answer by Bora Kasap · Mar 06, 2011 at 08:39 AM
it is working when jumping too, should be solved, work work work :!
Your answer
Follow this Question
Related Questions
Camera Bobbing and Stairs 4 Answers
Steam Vr and Camera Target Textures 0 Answers
Weapon Camera FOV 1 Answer
Shake camera up and down when running? 0 Answers