- Home /
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?
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
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!
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.
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?
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!
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.
Thank you very much!!! It's very much appreciated :)))
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!
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?
Your answer
Follow this Question
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