Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
avatar
Member [Lv. 2]
Member [Lv. 2]
Posts : 9
Points : 1328
Rep : 1
Join date : 2014-12-23
View user profilehttps://www.youtube.com/user/zinofxmovie/videos

head bobbing script Is very important in personal(free)

on Fri Dec 26, 2014 4:22 pm
Message reputation : 100% (1 vote)
head bobbing script Is very important in personal free


this script camera to move like your head when you walk in real life

dont forget subscribe my chanels youtube pleez [You must be registered and logged in to see this link.]

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;
}
}
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 1407
Rep : 0
Join date : 2014-09-15
View user profile

Re: head bobbing script Is very important in personal(free)

on Sat Jan 17, 2015 8:17 am
Wow, 12 errors.
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 28
Points : 1500
Rep : 2
Join date : 2014-10-05
Age : 16
Location : United Kingdom
View user profile

Re: head bobbing script Is very important in personal(free)

on Thu Mar 05, 2015 1:50 pm
Slandey wrote:Wow, 12 errors.
Sometimes there's problems with forums and indenting.  I'll make a pastebin myself later with corrections if you're that interested.

EDIT: Ok, this has clearly been extracted from another script with the headbob since some variables are missing, will still make a pastebin with *corrected* script.

EDIT: Hey, OP, this script works REALLY nicely! You just made a couple of minor(ish) errors when you posted the script here.
The *corrected* script is:
Code:
#pragma strict

/*
Code by Zino2K, corrected as a standalone script by Zino2K and Xezno.
For any information contact Zino2K on Unityninja.net
*/
private var timer = 0.0;
var bobbingSpeed = 0.18;
var bobbingAmount = 0.2;
var midpoint = 2.0;
private var waveslice = 0.0;
private var horizontal = 0.0;
private var vertical = 0.0;
private var translateChange = 0.0;
private var totalAxes = 0.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;
}
}
Not to offend you, of course, since you only missed a couple of variables.  Nothing much to worry about, happens to the best of us Rolling Eyes .  

Just attach this to your camera and it does everything for you.  You can adjust things such as the speed of the bobbing and the amount in-editor without editing code.
Back to top
Permissions in this forum:
You cannot reply to topics in this forum