I am trying to add headbobbing to my game, but none of them I found seemed to work well with my camera script and my playermovement script. I was wondering if there was a way that I can add headbobbing without the playermovement script and camera script getting in the way ?
Heres my playermovement and camerascript :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerlook : MonoBehaviour
{
[SerializeField] private float sensx;
[SerializeField] private float sensy;
Camera cam;
float mousex;
float mousey;
float multiplier = 0.01f;
float xrotation;
float yrotation;
public int stunt;
public int dead;
private void Start()
{
dead = gameObject.GetComponent<general>().dead;
stunt = gameObject.GetComponent<playermovement>().stunt;
cam = GetComponentInChildren<Camera>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
dead = gameObject.GetComponent<general>().dead;
stunt = gameObject.GetComponent<playermovement>().stunt;
if (dead == 0)
{
Myinput();
cam.transform.localRotation = Quaternion.Euler(xrotation, 0, 0);
transform.rotation = Quaternion.Euler(0, yrotation, 0);
}
else
{
dead = dead;
}
}
void Myinput()
{
mousex = Input.GetAxisRaw("Mouse X");
mousey = Input.GetAxisRaw("Mouse Y");
yrotation += mousex * sensx * multiplier;
xrotation += mousey * -sensy * multiplier;
xrotation = Mathf.Clamp(xrotation, -90f, 90f);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour
{
float playerheight = 2f;
[Header("Movement")]
public float movespeed = 3.5f;
float horizontalmovement;
float verticalmovement;
[SerializeField] float airMultiplier = 0.4f;
[Header("Keybinds")]
[SerializeField] KeyCode jumpKey = KeyCode.Space;
[Header("Jumping")]
public float jumpForce = 5f;
[Header("Drag")]
[SerializeField] float groundDrag = 6f;
[SerializeField] float airDrag = 2f;
bool isGrounded;
public bool moving;
float rbDrag = 6f;
float movementMultiplier = 10f;
public Vector3 movedirection;
Rigidbody rb;
public Animator anim;
public float time;
public int stunt;
public int dead;
public GameObject floor;
public int slipperyboi;
public float f = 500f; //sidewayforce
public float ff = -500f;
private void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
time = 0;
stunt = gameObject.GetComponent<general>().stunt;
dead = gameObject.GetComponent<general>().dead;
slipperyboi = floor.GetComponent<changingfloor>().slippery;
}
private void Update()
{
if (stunt == 1)
{
movespeed = 1f;
}
else
{
movespeed = 3.5f;
}
slipperyboi = floor.GetComponent<changingfloor>().slippery;
isGrounded = Physics.Raycast(transform.position, Vector3.down, playerheight / 2 + 0.1f);
//print(isGrounded);
//Myinput();
Myinput();
ControlDrag();
if (Input.GetKeyDown(jumpKey) && isGrounded)
{
if (stunt == 1)
{
stunt = 1;
}
else
{
Jump();
}
}
if (gameObject.GetComponent<Rigidbody>().velocity.magnitude > 0.01f)
{
moving = true;
}
else
{
moving = false;
}
anim.SetBool("movingornot", moving);
anim.SetBool("grounded", isGrounded);
}
void Jump()
{
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
void ControlDrag()
{
if (isGrounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = airDrag;
}
}
void Myinput()
{
horizontalmovement = Input.GetAxisRaw("Horizontal");
verticalmovement = Input.GetAxisRaw("Vertical");
movedirection = transform.forward * verticalmovement + transform.right * horizontalmovement;
}
private void FixedUpdate()
{
stunt = gameObject.GetComponent<general>().stunt;
dead = gameObject.GetComponent<general>().dead;
slipperyboi = floor.GetComponent<changingfloor>().slippery;
if (stunt == 1)
{
stunt = 1;
}
if (dead > 0)
{
dead = dead;
}
else
{
Moveplayer();
}
}
void Moveplayer()
{
if (isGrounded)
{
//rb.AddForce(movedirection * movespeed * movementMultiplier);
rb.AddForce(movedirection.normalized * movespeed * movementMultiplier, ForceMode.Acceleration);
//rb.AddForce(movedirection * movespeed * movementMultiplier, ForceMode.Acceleration);
}
else if (!isGrounded)
{
//rb.AddForce(movedirection * movespeed * movementMultiplier * airMultiplier);
rb.AddForce(movedirection.normalized * movespeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
//rb.AddForce(movedirection * movespeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
}
}
}