Im trying to make a camera bob sript that bobs on the z axis of a camera in between two values so my player walk looks more realistic, so far i have this for my script but im not getting much luck, below i the script i have for the camera bob. Is there something im doing wrong??
using UnityEngine;
using System.Collections;
public class CCameraBob : MonoBehaviour {
private float timer = 0.0f;
public float bobbingSpeed = 0.18f;
public float bobbingAmount = 0.2f;
public float midpoint = 2.0f;
void Update()
{
float waveslice = 0.0f;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 pos = transform.localPosition;
Quaternion rot = transform.rotation;
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 = (float)Mathf.Clamp(totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
pos.y = midpoint + translateChange;
rot.z = Mathf.PingPong(midpoint + translateChange, 20.5f);
}
else
{
pos.y = midpoint;
rot.z = midpoint;
}
transform.localPosition = pos;
transform.localRotation = rot;
}
}