Last active
January 20, 2023 01:38
-
-
Save deadasadodo/4116e6856f7663c4934b49cfeb48ac40 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SC_HeadBobber : MonoBehaviour | |
{ | |
public float walkingBobbingSpeed = 14f; | |
public float bobbingAmount = 0.05f; | |
public SC_CharacterController controller; | |
float defaultPosY = 0; | |
float timer = 0; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
defaultPosY = transform.localPosition.y; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if(Mathf.Abs(controller.moveDirection.x) > 0.1f || Mathf.Abs(controller.moveDirection.z) > 0.1f) | |
{ | |
//Player is moving | |
timer += Time.deltaTime * walkingBobbingSpeed; | |
transform.localPosition = new Vector3(transform.localPosition.x, defaultPosY + Mathf.Sin(timer) * bobbingAmount, transform.localPosition.z); | |
} | |
else | |
{ | |
//Idle | |
timer = 0; | |
transform.localPosition = new Vector3(transform.localPosition.x, Mathf.Lerp(transform.localPosition.y, defaultPosY, Time.deltaTime * walkingBobbingSpeed), transform.localPosition.z); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment