Files
DeviantMobile-Rohan/Assets/New Lowpoly Models/Scripts/CameraBob.cs
2026-06-26 22:41:39 +05:30

42 lines
909 B
C#

using UnityEngine;
public class CameraBob : MonoBehaviour
{
public float bobSpeed = 10f;
public float bobAmount = 0.05f;
private float timer;
private Vector3 startPos;
public CharacterController controller;
void Start()
{
startPos = transform.localPosition;
}
void Update()
{
if (controller == null) return;
if (controller.velocity.magnitude > 0.1f && controller.isGrounded)
{
timer += Time.deltaTime * bobSpeed;
float bobY = Mathf.Sin(timer) * bobAmount;
transform.localPosition = startPos + new Vector3(0, bobY, 0);
}
else
{
timer = 0f;
// smoothly reset
transform.localPosition = Vector3.Lerp(
transform.localPosition,
startPos,
Time.deltaTime * 5f
);
}
}
}