Headbobber

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
Line 3: Line 3:
 
[[Category: JavaScript]]
 
[[Category: JavaScript]]
 
[[Category: FPS]]
 
[[Category: FPS]]
 +
[[Category: First Person]]
 
Author: (Mr. Animator)
 
Author: (Mr. Animator)
 
==Description==
 
==Description==

Revision as of 02:36, 2 December 2005

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