Forum rules - please read before posting.

Switch headbob on and off

edited June 2023 in Technical Q&A

I want to be able to switch headbobbing on or off from the options menu, how would this be done correctly? I played around with the first person camera script for a bit

public void SetHeadbob() { if (headBob == true) { headBob = false; } else { headBob = true; } }

This works well enough but if the character is moving while its switched off the animation for the headbob stays on loop, and the selection isn't saving when loading a game.

Comments

  • If you're using the Custom Animation method, you can use an Animator Bool parameter and Transition to enter/exit your head-bobbing animation state.

    If you're instead using Built In, you could instead control the amplitude of the head-bob by affecting its bobbingAmount property:

    GetComponent<FirstPersonCamera> ().bobbingAmount = 0f;
    

    To save this value in your save-game, a custom Remember script would be needed. I can advise further on this, but let's see if the above is the right approach for you first.

  • I'm using Custom Animation method, I'm just unsure how to deal with the animator stuff as I couldn't get the bool value to change.

  • A Bool parameter can be changed with the Object: Animate Action, setting the Animation engine to Mecanim.

  • edited June 2023

    Right, my first instinct was to fiddle around with the code. Do I still need my original code for it to work? I'm trying to see if the value changes in the animator window but I can't see any changes. I was looking at the wrong place apparently.

  • No code should be needed. When dealing with the Animator, the animation playback is based on parameter values.

  • How do I set up the parameter and transition correctly? I've been trying but for the life of me can't get it working properly. I'm using the downloadable first person player prefab as a base for my character.

  • Create a Bool parameter in the Animator named e.g. "Stop Headbob", and also add a new empty state in the Base layer.

    Then create a transition from "Any State" to this new empty state, with "Stop Headbob = true" as a condition.

    From the empty state to "Idle", do the opposite - a transition with "Stop Headbob = false".

    You can then use the Object: Animate Action to set the value of "Stop Headbob".

  • edited June 2023

    Right, that works. Thanks Chris! Did this method need any custom saving to remember the change?

  • If you want to save the state of the "Stop Headbob" parameter, attach AC's Remember Animator component - that'll keep a record of all the Animator's parameter values.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.