Forum rules - please read before posting.

[Feature Request] Fist person Parabolic headbobbing. And maybe crouching.

Hi, I've recently had my attention drawn by the first person camera script in the API and I was wondering if there's a way to include an alternative headbobbing style in the code? The current technique is the most standard and widely used style, which is a little unnatural (sometimes it almost feels like the player is jumping up and down, lol.) So I was wondering if we can add parabolic head bobbing (which functions more or less in an arc). The movement in this style/technique is a bit more natural and pleasant (course, all headbobbing is irritating if used too strong...). 

I actually added a feature like that in a project a few weeks ago (both crouching and head bobbing)... Originally, I was just going to add crouching but I noticed the AC's head bobbing took complete control over the camera so I had to turn it off. And if I wanted headbobbing at all I'd had to add it myself, so I had to put both features in. Still, I felt it was a bit of a loss because that meant I could't use some of the native AC features for the Camera. And I feel it would be nice if there was some basic crouching feature already there. It doesn't need to be fancy though.

Anyway, I make both request in one space because as you probably already know both features involve camera movement so it's something that needs to be worked together in most cases.

Comments

  • edited October 2016
    By the way, here's the example I based myself for my own version of the parabolic headbob (it ended up looking pretty different,though...) still, it may give you ideas, although, I know you're a much, much better programmer than me so it may be unnecessary xD, but here it is:

    public class Headbob : MonoBehaviour {

    public Vector3 restPosition; //local position where your camera would rest when it's not bobbing.
    public float transitionSpeed = 20f; //smooths out the transition from moving to not moving.
    public float bobSpeed = 4.8f; //how quickly the player's head bobs.
    public float bobAmount = 0.05f; //how dramatic the bob is. Increasing this in conjunction with bobSpeed gives a nice effect for sprinting.

    float timer = Mathf.PI / 2; //initialized as this value because this is where sin = 1. So, this will make the camera always start at the crest of the sin wave, simulating someone picking up their foot and starting to walk--you experience a bob upwards when you start walking as your foot pushes off the ground, the left and right bobs come as you walk.
    Vector3 camPos;

    void Awake()
    {
    camPos = transform.localPosition;
    }

    void Update()
    {
    if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0) //moving
    {
    timer += bobSpeed * Time.deltaTime;

    //use the timer value to set the position
    Vector3 newPosition = new Vector3(Mathf.Cos(timer) * bobAmount, restPosition.y + Mathf.Abs((Mathf.Sin(timer) * bobAmount)), restPosition.z); //abs val of y for a parabolic path
    camPos = newPosition;
    }
    else
    {
    timer = Mathf.PI / 2; //reinitialize

    Vector3 newPosition = new Vector3(Mathf.Lerp(camPos.x, restPosition.x, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.y, restPosition.y, transitionSpeed * Time.deltaTime), Mathf.Lerp(camPos.z, restPosition.z, transitionSpeed * Time.deltaTime)); //transition smoothly from walking to stopping.
    camPos = newPosition;
    }

    if (timer > Mathf.PI * 2) //completed a full cycle on the unit circle. Reset to 0 to avoid bloated values.
    timer = 0;
    }
    }
  • edited October 2016
    I'll take a look.  AC's internal First Person mode will always be quite basic, so crouching etc is best done through a custom motion controller or integration with e.g. UFPS.  Head-bobbing options could be added, but ideally it'd come in the form of an animation curve you can tweak for minimal fields, but I'll test your script and see how well it plays.
  • edited October 2016
    Alright... though that's not "my script" that's one I found in the Unity forums, I don't really remember how much I had to change at the end to get it working how I wanted, and I didn't want to post the end result because I was a bit hacky getting both crouch and headbobbing to work together xD (at least I sort of remember it felt that way)... but I guess I'll post the script anyway. You can see my final code here:


    There's actually one more script. That one was simply in charge of verifying if the character is under something to change the SafetyCrouch variable (to make sure the player wouldn't stand up in a place where it was not possible to do so-- There's also a ForceCrouhc used in a custom AC action so that the user can force crouching in cutscenes, etc). But just to warn you I've been doing mobile recently and I've been sort of obsessed with avoiding some calculations which in large numbers may slow down the game, like raycasting, so I tried a rather interesting but slightly inaccurate technique to figure it out if an objects collider is being hit at an specific side. That way I can do ray casting only where there's actual contact on the "head" and stop when there isn't (lol, I'm not actually sure if it ends up being any faster but it sure was fun trying it xD). Anyway this is that script in case you are interested:



  • edited October 2016
    It would be good addition, of course, if it is possible to implement! For now, head boobing in the Ac is quite simple and rather unnatural!. It would be nice if some more options existed within the AC directly. Such an option would be ideal for horror games..
  • In my opinion, the best option would be to provide a way to trigger an Animator parameter so that you can play whatever animation / combination of animations you like, much in the same way you control character animation with Mecanim.
  • edited October 2016
    Weird, the links are already dead, I though they were supposed to last a week... Anyway, back to the discussion... 

    Well, to be honest animating head bobbing to look right/natural would be in itself quite a task... it's probably the reason why it's usually done through scripting, not animation... It's that kind of thing that's usually scripted because it's easier that way or because the result is simply better looking. It's like a light's flickering, the most natural ones are always randomized in some way (by script). An animation will simply never compare, specially if realism is part of the goal... but anyway, I was just saying it'd be nice to have stuff like this natively... Specially because there's tons of non programmers using AC...
  • edited October 2016
    I agree with Alverik . It would be useful in many directions. Especially for fps! Of course, if it is not difficult to implement..
  • All points I'll take into account.
  • I would definitely love to have crouching implemented for the first person controller at least. I've run into this limitation before with AC and it would definitely be a useful option to have.
  • A bit late to the party I know, but could I add a belated vote for built in First Person crouching too please? I'll probably try and hack something together myself, because I've got no requirement for it to be super realistic, just that the camera dips down when the Player does, but I think it would be a useful feature for AC to have out of the box

  • I've got no requirement for it to be super realistic, just that the camera dips down when the Player does

    If that's all you need, you can animate the camera's position when the player presses an Active Input.

    For something more involved, however, try this:
    http://pasteall.org/1447134/csharp

  • edited January 2019

    thanks for that chris

    i've already implemented a really simple (so simple i'd be ashamed to share it) function to dip the FP camera when the Player's "Crouch" animation is playing (rather than checking for input), but I'll definitely have a play around with this because it would be neat to let the player have the ability to consciously crouch their way under obstacles and whatnot

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.