So i’ve been trying to create a basic project for a 3d First Person Action Game. I’ve implemented my own little Character Controller and a script that handels camerafollow and rotations. But now when i move in the game and look around at the same time, the camera stutters.
I’ve uploaded a prototype, if you want to see it for yourself.
And here the Camera Script:
public class CameraController : MonoBehaviour {
public GameObject Target;
[Header("Mouse Sensetivity")]
public float sensitivityX = 2f;
public float sensitivityY = 2f;
public float sensXAiming = 2f;
public float sensYAiming = 2f;
public bool aiming = false;
[Header("Possible Looking Angles")]
public float minimumY = -60f;
public float maximumY = 60f;
[Header("Number of Frames to average")]
public int frameCounterX = 35;
public int frameCounterY = 35;
//Mouse rotation input
private float rotationX = 0f;
private float rotationY = 0f;
//Used to calculate the rotation of this object
private Quaternion xQuaternion;
private Quaternion yQuaternion;
private Quaternion originalRotation;
//Array of rotations to be averaged
private List<float> rotArrayX = new List<float>();
private List<float> rotArrayY = new List<float>();
void Start () {
Rigidbody rb = GetComponent<Rigidbody>();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update () {
RotateCamera();
FollowTarget();
}
void FollowTarget()
{
GetComponent<Rigidbody>().MovePosition(
new Vector3(
Target.transform.position.x,
Target.transform.position.y + Target.GetComponent<CharacterControllerCustom>().hNormal / 2,
Target.transform.position.z) - transform.forward * 0.25f
);
}
void RotateCamera()
{
//Average rotationX for smooth mouselook
float rotAverageX = 0f;
if (!aiming)
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
else
rotationX += Input.GetAxis("Mouse X") * sensXAiming;
//Add the current rotation to the array, at the last position
rotArrayX.Add(rotationX);
//Reached max number of steps? Remove the oldest rotation from the array
if (rotArrayX.Count >= frameCounterX)
{
rotArrayX.RemoveAt(0);
}
//Add all of these rotations together
for (int i_counterX = 0; i_counterX < rotArrayX.Count; i_counterX++)
{
//Loop through the array
rotAverageX += rotArrayX[i_counterX];
}
//Now divide by the number of rotations by the number of elements to get the average
rotAverageX /= rotArrayX.Count;
//Average rotationY, same process as above
float rotAverageY = 0;
if (!aiming)
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
else
rotationY += Input.GetAxis("Mouse Y") * sensYAiming;
rotationY = ClampAngle(rotationY, minimumY, maximumY);
rotArrayY.Add(rotationY);
if (rotArrayY.Count >= frameCounterY)
rotArrayY.RemoveAt(0);
for (int i_counterY = 0; i_counterY < rotArrayY.Count; i_counterY++)
rotAverageY += rotArrayY[i_counterY];
rotAverageY /= rotArrayY.Count;
//Apply and rotate this object
xQuaternion = Quaternion.AngleAxis(rotAverageX, Vector3.up);
yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
private float ClampAngle(float angle, float min, float max)
{
if (angle < -360f)
angle += 360f;
if (angle > 360f)
angle -= 360f;
return Mathf.Clamp(angle, min, max);
}
}
I’m using Unity version 5.5.0f3 at the moment.
Any idea what i’m doing wrong?