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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » FREE Project Downloads & Recources » Scripts » head bobbing script Is very important in personal(free)

head bobbing script Is very important in personal(free)

View previous topic View next topic Go down  Message [Page 1 of 1]

zino2k


Member [Lv. 2]
Member [Lv. 2]
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;
}
}

View user profile https://www.youtube.com/user/zinofxmovie/videos

Slandey


Member [Lv. 0]
Member [Lv. 0]
Wow, 12 errors.

View user profile

4kbytes. (xezno)


Member [Lv. 3]
Member [Lv. 3]
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.

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum