How do i get this to bob in the x axis?

Hey guys this may sound stupid but i was wondering if you guys could help me out :). Well basically i was wondering how would i get this head bobbing script to bob along the x axis at the moment this bobs on the Y :slight_smile:

here is the script which was found from unity wiki thank you so much in advance MCHALO :slight_smile:

private var timer = 0.0; 

var bobbingSpeed = 0.18;

var bobbingAmount = 0.2;

var midpoint = 0.1;

function Update () {

var waveslice : float  = 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) { 

   var translateChange = waveslice * bobbingAmount; 

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

} 

}

This bobbing script is somewhat confusing, and hard to adapt - it’s easier to create a new one. The script below allows bobbing in any direction, and with any amplitude: the vector bobbingDir sets the bobbing amount in each axis separately. Furthermore, it rotates the bobbingDir vector to the transform orientation: if you specify bobbingDir.x = 0.1, for instance, it will bob laterally 0.1 to each side - no matter to which direction the head is turned. Another feature: it always does complete bobbing cycles - if you release the controls, it finish the current cycle and stops.

var bobbingSpeed: float = 1.5;  
var bobbingDir: Vector3 = Vector3(0.1, 0, 0); 
private var initPos: Vector3; 

function Start(){
	initPos = transform.localPosition;
}

function Update(){
	var axes = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
	if (axes.magnitude>0.01) DoBobbing(); // do bobbing cycles while controls active
}

private var bobbing = false;
private var TWOPI = Mathf.PI*2;

function DoBobbing(){
	if (bobbing) return; // only start a new bobbing cycle if the previous has ended
	bobbing = true; // starting new bobbing cycle
	for (var t:float = 0; t<1.0;){
		t += Time.deltaTime*bobbingSpeed;
		movement = transform.TransformDirection(bobbingDir)*Mathf.Sin(TWOPI*t);
		transform.localPosition = initPos+movement;
		yield; // return here next frame until cycle ends
	}
	bobbing = false; // cycle finished
}

EDITED: The version below is simpler: it just bobs while the controls are active, and stops as soon as they are released:

var bobbingSpeed: float = 1.5;  
var bobbingDir: Vector3 = Vector3(0.1, 0, 0); 
private var initPos: Vector3;
private var bobTime: float = 0;
private var TWOPI = Mathf.PI*2;

function Start(){
    initPos = transform.localPosition;
}

function Update(){
    var axes = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    if (axes.magnitude>0.01){
        bobTime += Time.deltaTime*bobbingSpeed;
        movement = transform.TransformDirection(bobbingDir)*Mathf.Sin(TWOPI*bobTime);
        transform.localPosition = initPos+movement;
    }
}