I copied somebodies script for a head bobbing motion as you walk because I’m rusty as hell with coding, but It overrides my ability to jump. I think it may be because of the axes clamp (line 53), but I’m not sure.
*** I included the jumping with the head bob script as part of my trial and error process to fix this but even when I had it in my walking script, the same issue occurred.
Here is the script:
public class Headbobber : MonoBehaviour
{
private float timer = 0.0f;
float bobbingSpeed = 0.08f;
float bobbingAmount = 0.04f;
float midpoint = .43f;
private Rigidbody rb;
public CapsuleCollider col;
public LayerMask groundLayers;
public float jumpForce;
void Start()
{
rb = GetComponent<Rigidbody>();
col = GetComponent<CapsuleCollider>();
}
void Update()
{
float waveslice = 0.0f;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 cSharpConversion = transform.localPosition;
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
{
timer = 0.0f;
}
else
{
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2)
{
timer = timer - (Mathf.PI * 2);
}
}
if (waveslice != 0)
{
float translateChange = waveslice * bobbingAmount;
float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
cSharpConversion.y = midpoint + translateChange;
}
else
{
cSharpConversion.y = midpoint;
}
transform.localPosition = cSharpConversion;
if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
private bool IsGrounded()
{
return Physics.CheckCapsule(col.bounds.center, new Vector3(col.bounds.center.x,
col.bounds.min.y, col.bounds.center.z), col.radius * .3f, groundLayers);
}
}