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
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;
}
}
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
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
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