Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Kazaskater · Dec 04, 2010 at 07:55 PM · camerafpswalkshakebobbing

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Eric5h5 · Dec 04, 2010 at 07:58 PM

Try this script: http://www.unifycommunity.com/wiki/index.php?title=Headbobber

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kazaskater · Dec 05, 2010 at 01:10 AM 1
Share

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.

avatar image max 1 · Mar 30, 2011 at 03:51 PM 0
Share

thanks big time

avatar image bumbummen99 · Sep 30, 2013 at 05:46 PM 0
Share

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)
avatar image TheReclaimer117 · Feb 02, 2014 at 07:41 AM 0
Share

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.

avatar image american00b · Jul 29, 2014 at 01:56 PM 0
Share

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?

Show more comments
avatar image
1

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 );
  }    
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image angrycarrots · Feb 15, 2013 at 07:03 AM 0
Share

Thanks so much I have been looking everywhere online for a c# bobbing script. Do I have permission to use this?? Thanks

avatar image friketrike · Sep 30, 2013 at 06:24 PM 0
Share

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

avatar image Something_Fishy · Mar 23, 2014 at 04:35 PM 0
Share

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?

avatar image
0

Answer by Bora Kasap · Mar 06, 2011 at 08:39 AM

it is working when jumping too, should be solved, work work work :!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

My Camera won't activate trough script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges