Headbobber

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Removing all content from page)
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Aarku)
Line 1: Line 1:
 +
[[Category: Camera]]
 +
[[Category: MonoBehaviour]]
 +
[[Category: JavaScript]]
 +
[[Category: FPS]]
 +
[[Category: First Person]]
 +
Author: (Mr. Animator)
 +
==Description==
 +
This script makes an object bob up and down smoothly when you're pressing one or both of the horizontal/vertical axes. Attach it to a camera and play with the public variables to get the feel of your player's head bobbing as they walk. The midpoint variable is whatever value for the Y translate you want to be considered the middle of the camera's bobbing range.
 +
==Usage==
 +
Place this script on a Camera.
  
 +
==JavaScript - Headbobber.js==
 +
<javascript>
 +
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;
 +
    }
 +
}
 +
</javascript>

Revision as of 19:07, 19 October 2009

Author: (Mr. Animator)

Description

This script makes an object bob up and down smoothly when you're pressing one or both of the horizontal/vertical axes. Attach it to a camera and play with the public variables to get the feel of your player's head bobbing as they walk. The midpoint variable is whatever value for the Y translate you want to be considered the middle of the camera's bobbing range.

Usage

Place this script on a Camera.

JavaScript - Headbobber.js

<javascript>

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; 
   } 
}

</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox