Headbobber

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<javascript>" to "<syntaxhighlight lang="javascript">")
m (Text replace - "</javascript>" to "</syntaxhighlight>")
 
Line 39: Line 39:
 
     }  
 
     }  
 
  }
 
  }
</javascript>
+
</syntaxhighlight>
  
 
[[Category: Camera]]
 
[[Category: Camera]]

Latest revision as of 20:52, 10 January 2012


Author: (Mr. Animator)

[edit] 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.

[edit] Usage

Place this script on a Camera.

[edit] JavaScript - Headbobber.js

 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; 
    } 
 }
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox