x


Headbob Script

So ive been wanting to make a "head bob" script for my fps character. I found one example of one on a website. It was in javaScript so i converted it to C# the best i could(im making the game for the flash platform so i dont want to deal with javaScript). I put it on my fps player's camera. However it has NO EFFECT on the camera. Can anyone tell me why this is? Here is my script:

using UnityEngine;
using System.Collections;

public class FPSHeadBob : MonoBehaviour {

private float timer = 0.0f;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;

void Update () {
    float lpY = transform.localPosition.y;
    float waveslice = 0.0f;
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");
    if(Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
    {
       timer = 0.0f;
    }
    else
    {
       waveslice = Mathf.Sin(timer);
       timer = timer + bobbingSpeed;
       if(timer > Mathf.PI * 2)
       {
         timer = timer - (Mathf.PI * 2);
       }
    }
    if(waveslice != 0)
    {
       float translateChange = waveslice * bobbingAmount;
       float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
       totalAxes = Mathf.Clamp (totalAxes, 0.0f, 1.0f);
       translateChange = totalAxes * translateChange;
       lpY = midpoint + translateChange;
    }
    else
    {
       lpY = midpoint;
    }
}
}

Any help is appreciated.

more ▼

asked Apr 12 '12 at 12:38 AM

DGArtistsInc gravatar image

DGArtistsInc
269 8 15 19

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Hi!

IpY is no pointer to the transform y or something. The calculated value is never written back to the actual transform.

The last lines in Update must be something like:

Vector3 newPos = transform.localPosition;
newPos.y += IpY;
transform.localPosition = newPos;

or a bit more compressed

transform.localPosition = transform.localPosition + transform.up * IpY;

But that may not lead to a result also, because the actual movement happens somewhere else. You may need to change the Controller script directly and apply it in the calculation there, otherwise you never know which script is calculated first.

Good luck

more ▼

answered Apr 12 '12 at 02:49 AM

captaincrunch80 gravatar image

captaincrunch80
240 1 3 4

yes, thanks for the reply it did actually cause a result... my camera on the fps controller shot directly upward. Heres what i put down:

if(waveslice != 0)

{

float translateChange = waveslice * bobbingAmount;

float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);

totalAxes = Mathf.Clamp (totalAxes, 0.0f, 1.0f);

translateChange = totalAxes * translateChange;

Vector3 newPos = transform.localPosition;

newPos.y += lpy;

transform.localPosition = newPos;

}

else

{

lpy = midpoint;

}

Apr 15 '12 at 07:17 PM DGArtistsInc
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3014
x1175
x6

asked: Apr 12 '12 at 12:38 AM

Seen: 1128 times

Last Updated: Apr 18 '12 at 05:26 PM