x


Headbobber script in C#?

http://unifycommunity.com/wiki/index.php?title=Headbobber

Does anybody have a camera bob script like the one I posted above, but in C#? I am fairly new and I have no idea how to convert it. I planned on adding onto it with different bobbing speeds for how fast my character is sprinting, but I do not know JavaScript at all. I also need to access variables from another C# script to get my run speed, and I'm not sure if you can do that in JS either...

more ▼

asked Jul 12 '12 at 01:14 AM

Josh707 gravatar image

Josh707
66 5 11 15

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

1 answer: sort voted first

Looks like this would be a converted version:

public class Headbobber: MonoBehaviour 
{

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

 void Update () {
    float waveslice = 0.0f;
    float horizontal = Input.GetAxis("Horizontal");
    float vertical = Input.GetAxis("Vertical");

 Vector3 cSharpConversion = transform.localPosition; 

    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;
       cSharpConversion.y = midpoint + translateChange;
    }
    else {
       cSharpConversion.y = midpoint;
    }

 transform.localPosition = cSharpConversion;
 }



}

It's hard to know because it seems undefined variables pop up out of no where. Unfortunately I don't have any projects to test this code for you, but the console doesn't give any compile errors for me. Also, the cSharpConversion V3 is there because c# doesn't like transform.localPosition.y = midpoint; :(

Happy coding.

more ▼

answered Jul 12 '12 at 02:11 AM

Muuskii gravatar image

Muuskii
1.2k 4 10

This seems pretty much the same as the JS one, I tested it quickly and it works! Thank you.

Jul 12 '12 at 03:11 AM Josh707

Thank you for accepting my answer. It's no problem really I was just banging my head from my own project not working anyway xD

Jul 12 '12 at 03:13 AM Muuskii
(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:

x4371
x3125
x151
x9
x7

asked: Jul 12 '12 at 01:14 AM

Seen: 708 times

Last Updated: Jul 12 '12 at 03:13 AM