- Home /
FP Camera how to crouch and headbob at the same time
Hi, I wanted to know how properly use both headbob and crouching in FP camera at the same time. I'm somewhat new to unity and programming and I've never done any first person before, so I'm having a little trouble. this is what I have right now:
public class FpsCrouch : MonoBehaviour
{
public float CrouchHeight = .9f;
public float CrouchSmoothTime = 0.3f;
protected Vector3 _baseCamHeight;
private Vector3 _crouchCamHeight;
protected Vector3 _currentVelocity;
private float _currentVelocityB;
public static bool ForceCrouch;
private AC.Player _player;
private CapsuleCollider _playerCollider;
private float _playerWalkSpeed = 0f;
private float _playerRunSpeed = 0f;
private bool _speedWasChanged;
private float _originalColliderSize;
private float _targetColliderSize;
private Vector3 _originalColliderCenter;
private Vector3 _targetColliderCenter;
public Vector3 TargetPosition;
protected virtual void Awake()
{
// Assume starting height is standing height.
_baseCamHeight = transform.localPosition;
// Move that position down to find crouching position.
_crouchCamHeight = transform.localPosition;
_crouchCamHeight.y = CrouchHeight;
}
protected virtual void Start()
{
_player = GameObject.FindGameObjectWithTag("Player").GetComponent<AC.Player>();
_playerWalkSpeed = _player.walkSpeedScale;
_playerRunSpeed = _player.runSpeedScale;
_playerCollider = _player.GetComponent<CapsuleCollider>();
_originalColliderSize = _playerCollider.height;
_originalColliderCenter = _playerCollider.center;
}
protected virtual void Update()
{
//if the player holds the crouch button
//or the system enforces a crouch state, then crouch
if (Input.GetButton("Crouch") || ForceCrouch)
{
TargetPosition.y = _crouchCamHeight.y;
_targetColliderSize = _originalColliderSize / 1.5f;
_targetColliderCenter = _originalColliderCenter / 2;
//if the speed wasn't changed yet.
if (!_speedWasChanged)
{
//change it
_player.runSpeedScale = _playerWalkSpeed;
_speedWasChanged = true;
}
CrouchTransition(TargetPosition);
}
else
{
TargetPosition.y = _baseCamHeight.y;
_targetColliderSize = _originalColliderSize;
_targetColliderCenter = _originalColliderCenter;
//if the speed was changed before. Reset it.
if (_speedWasChanged)
{
_player.runSpeedScale = _playerRunSpeed;
_speedWasChanged = false;
}
CrouchTransition(TargetPosition);
}
}
protected void CrouchTransition(Vector3 targetPosition)
{
//transition the collider's position
_playerCollider.height = Mathf.SmoothDamp(_playerCollider.height, _targetColliderSize,
ref _currentVelocityB,
CrouchSmoothTime);
//transition the collider's center
_playerCollider.center = Vector3.SmoothDamp(
_playerCollider.center,
_targetColliderCenter,
ref _currentVelocity,
CrouchSmoothTime);
//transition the camera to the appropriate position.
transform.localPosition = Vector3.SmoothDamp(
transform.localPosition,
targetPosition,
ref _currentVelocity,
CrouchSmoothTime
);
}
}
and:
public class HeadBob : FpsCrouch
{
//local position where your camera would rest when it's not bobbing.
private Vector3 restPosition;
//smooths out the transition from moving to not moving.
public float transitionSpeed = 20f;
//how quickly the player's head bobs.
public float bobSpeed = 4.8f;
//how dramatic the bob is. Increasing this in conjunction
//with bobSpeed gives a nice effect for sprinting.
public float bobAmount = 0.1f;
//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.
float timer = Mathf.PI / 2;
Vector3 camPos;
protected override void Awake()
{
restPosition = new Vector3(0, transform.localPosition.y, 0);
camPos = transform.localPosition;
base.Awake();
}
protected override void Update()
{
base.Update();
if (!(Math.Abs(transform.localPosition.y - _baseCamHeight.y) < 0.03f) && (Input.GetButton("Crouch") || ForceCrouch)) return;
if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) //moving
{
timer += bobSpeed * Time.deltaTime;
//use the timer value to set the position
//if you're not crouching
if ((Input.GetButton("Crouch") || ForceCrouch))
{
return;
}
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
transform.localPosition = newPosition;
Debug.Log("I'm bobbing!");
}
else
{
timer = Mathf.PI / 2; //reinitialize
if ((Input.GetButton("Crouch") || ForceCrouch))
{
return;
}
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));
transform.localPosition = newPosition;
}
if (timer > Mathf.PI * 2) //completed a full cycle on the unit circle. Reset to 0 to avoid bloated values.
timer = 0;
}
}
I put this together using several examples I found on the forums as a base, then modified some stuff to my taste. But, at first they only worked by themselves. One always blocked the other, so I tried to make some changes to allow one to know if crouching was taking place, but the problem is, this way headbobbing only works while not crouching and also there's a sudden "jump" in the camera when you release the crouch button while you are walking or running...
So, how would I go about merging both camera smoothdamp operations? I do really love the parabollic headbob sway, but I'm a little confused about how I'd use it while crouching too.
Anyway, any advice will be greatly apprectiated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Character moves slightly while crouching? 0 Answers