- Home /
Head bob script overrides ability to jump,Not able to jump with Head bobbing script
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);
}
}
Answer by iveL_newO · Apr 29, 2020 at 07:59 AM
Your head bobbing script is setting your player's y position; try putting the head bob on your camera while keeping everything else on the player, then make the camera's parent the player if you haven't already.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Still constant jumping even with raycasting. 0 Answers
,How to disable autojumping when holding spacebar? 4 Answers
I cant make my Character jump. 0 Answers