Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by handsomemuffin · Jun 22, 2011 at 03:30 PM · camerafpsbobbing

Camera Bobbing and Stairs

Hi, Im creating a simple enviroment for a college project. I have added this script http://www.unifycommunity.com/wiki/index.php?title=Headbobber to my character so its head will bob. After doing this I can no longer walk downstairs. What can I do to fix this?

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

4 Replies

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

Answer by SilverTabby · Jun 22, 2011 at 05:26 PM

Your problem is in this little bit:



transform.localPosition.y = midpoint + translateChange;
    }
    else {
       transform.localPosition.y = midpoint;

Every single frame you are setting your y value to 2. This means that your transform will NEVER be able to fall, jump, or climb anything. You will always be at exactly y = 2.

Try this (edited from your script - mostly unchanged):


private var timer = 0.0; //used as the input to the sine function private var translateChange = 0; //How far the camera is away from it's resting point private var lastTranslateChange = 0; //used to keep track of the delta (change in) translation

var bobbingSpeed = 0.18; //How quickly the camera bobs up and down var bobbingAmount = 0.2; //How high/low the camera's max/min bob is. var settlingRate = 1; //determines how fast we return to 0 position while standing still

function Update () { var waveslice = 0.0; //determine how fast the player wants to move var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical");

 //first, grab a sine value to work with
      //not moving --> return to midpoint
 if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0) {
    timer = 0.0;
 }
 else {
        //we are moving, grab a bob value from the sine curve
    waveslice = Mathf.Sin(timer);
    timer = timer + bobbingSpeed;   //move along the sine curve for the next cycle
    if (timer > Mathf.PI * 2) {
       timer = timer - (Mathf.PI * 2); // sine repeats after 2PI. Keep timer in a reasonable range
    }
 }

 //now calculate how much we want to move the camera
     //we ARE moving
 if(waveslice != 0)
 {           //bob up and down based on a sine wave and movement amount
    translateChange = waveslice * bobbingAmount;    //scale translation by bobbing amount
    totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
    totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
    translateChange = totalAxes * translateChange;   //scale translation by movement amount
 }
 else
 {      //we are sitting still, smoothly return to 0 position
     translateChange = Mathf.Lerp(lastTranslateChange, 0, Time.deltaTime * settlingRate);
 }

 //now actually move the camera
     //move the transform's y component by the delta translation
 transform.localPosition.y += translateChange - lastTranslateChange;
 lastTranslateChange = translateChange;    //keep track of last cycle's translation

}


Edit: changed/added comments as requested

Comment
Add comment · Show 4 · 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 handsomemuffin · Jun 22, 2011 at 05:33 PM 0
Share

I only wish I could take credit for it haha! It was made by someone over on the wiki, however what you have added makes it work like a charm! Thanks alot for your help!

avatar image ephdisc · Mar 31, 2012 at 02:49 AM 0
Share

Looking at this bit of code has been very helpful. One issue I saw was that the private variable translateChange = 0;

then when you get to

translateChange = totalAxes * translateChange;

and translateChange always becomes 0. So I would guess that translateChange should be a non 0 float. I don't know if this change would cause some type of crawl though.

avatar image Hordaland · Apr 19, 2012 at 02:41 AM 0
Share

I have a slight problem with this script. The head bobs only if bobbingAmount is more than 2 which results in very much bobbing, to say the least. Anyone else had this problem?

avatar image Eldirfar · Apr 04, 2014 at 08:22 AM 0
Share

Great, work as excepted. Thanks

avatar image
0

Answer by thetinman101 · Oct 21, 2011 at 07:02 AM

This is great! thank you very much!! Would you be kind enough to just add comments to each line so it's easier to understand exactly what's going on?

Thanks a lot!

Comment
Add comment · Show 2 · 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 SilverTabby · Oct 21, 2011 at 05:14 PM 0
Share

There. I tried to add comments to explain what is going on, but I fear that you won't be able to understand it without a Trigonometry class.

Each individual step isn't complex, the hard part is understanding the sine curve { $$anonymous$$athf.Sin() } inside and out.

avatar image thetinman101 · Oct 21, 2011 at 05:49 PM 0
Share

Thank you very much!!! It's very much appreciated :)))

avatar image
0

Answer by thetinman101 · Oct 21, 2011 at 07:02 AM

This is great! thank you very much!! Would you be kind enough to just add comments to each line so it's easier to understand exactly what's going on?

Thanks a lot!

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
avatar image
0

Answer by nazia08 · Apr 07, 2014 at 10:53 AM

i've noticed that all the camera men wear a jacket that says BOB on the back..what does it mean? does it represent something?

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 Height-based crouch script 2 Answers

How would I get camera bobbing? 0 Answers

how can i make my character jump? 5 Answers

How do I make a camera bob when I walk? 3 Answers

FPS camera script - keeps reverting to 0,0,0 rotation 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