x


Headbob script ruins character, help?

My headbob script I got from wiki unity community ruins my character. When I press play his arms are stretched WAY out in front of him, they are child objects of the main camera. I need them to be child objects because it is a FPS. How can I make it only effect the camera though? Here is code:

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 += bobbingSpeed * Time.deltaTime;
       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; 
    } 
 }
more ▼

asked Sep 04 '12 at 03:39 AM

bpears gravatar image

bpears
168 8 22 51

sorry bout that

Sep 04 '12 at 07:26 PM bpears
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

One option would be to have your mouselook scripts attached to an object that is a parent to both the camera and arms like this:

 - Parent
   - Camera
   - List item

Then you can attach the camera bob script to the camera and it won't effect the arms at all.

more ▼

answered Sep 04 '12 at 10:21 PM

drizztmainsword gravatar image

drizztmainsword
243 1 4 5

I tried this (attached it to the main parent of entire character) and now my character falls through the terrain... not sure how this is happening, even if i raise him higher he still falls through.

Sep 05 '12 at 03:28 AM bpears

That's odd...

You don't want the camera to change the vertical (X) rotation of your character though, just the camera and arms.

What object is your Character Controller on?

Sep 05 '12 at 03:46 AM drizztmainsword

CharacterController is on entire player. His arms aren't getting stretched out all weird anymore, but he falls through... weird..

Sep 05 '12 at 04:06 AM bpears

the thing is, I cant separate his arms and camera from body, because the movement is related to the rest of body.

Sep 05 '12 at 04:12 AM bpears
(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:

x1172
x11
x9
x6

asked: Sep 04 '12 at 03:39 AM

Seen: 275 times

Last Updated: Sep 05 '12 at 04:12 AM