FPS Camera Effects

Hello all, recently I’ve been messing around with the FPS walker script and have tried expanding upon it. Strafing causes the camera to tilt in the corresponding direction and moving forward sways the camera back-and-forth. But for some strange reason whenever the camera is moved, it becomes somewhat jittery depending on the X angle of the camera. My guess is that it has to do with “controller.Move”. Could anyone help rid of this problem?

var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
var rspeed = 1.0;
var rx = 0.0;
var ry = 0.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var zrot = 0.0;
private var sway : boolean = false;
private var moving : boolean = false;

function FixedUpdate()
{
    if(grounded)
    {
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
        
        if(Input.GetButton ("Jump")) moveDirection.y = jumpSpeed;
    }
	else moveDirection.y -= gravity * Time.deltaTime;
		
    var controller : CharacterController = GetComponent(CharacterController);	
	var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    
    FPS();
}

function FPS()
{
    rx += Input.GetAxis("Mouse X") * rspeed;
    ry -= Input.GetAxis("Mouse Y") * rspeed;
    
    CameraShake();
    
    transform.rotation = Quaternion.Euler(ry, rx, zrot);
}

function CameraShake()
{        
    if(grounded)
    {
        var vert = Input.GetAxis("Vertical");
        var hori = Input.GetAxis("Horizontal");
        
        moving = (vert != 0 || hori != 0);
        
        if(hori != 0)
        {
            if(hori > 0)
                if(zrot-- <= -10.0)
                    zrot = -10.0;
                
            if(hori< 0)
                if(zrot++ >= 10.0)
                    zrot = 10.0;
                    
            return;
        }
        
        if(vert > 0)
        {
            if(!sway)
            {
                if(zrot++ >= 10.0)
                    sway = true;
            }
            else
            {
                if(zrot-- <= -10.0)
                    sway = false;
            }
        }
    }
    else moving = false;
    
    if(!moving)
    {
        if(zrot < 0)
            zrot++;
        else if(zrot > 0)
            zrot--;
    }
}

@script RequireComponent(CharacterController)

Okay guys, I’ve stopped using Unity’s Character Controller entirely, and here’s what I’ve got:

var speed = 0.3;
var jumpSpeed = 0.3;
var gravity = 1.0;
var rspeed = 4.0;
var rx = 0.0;
var ry = 0.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var zrot = 0.0;
private var sway : boolean = false;
private var moving : boolean = false;

function FixedUpdate()
{	
	var hit : RaycastHit;
    
	if(grounded)
    {				
        moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
       
		moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
		moveDirection.y = 0.0;
		
        if(Input.GetButton ("Jump")) moveDirection.y = jumpSpeed;
    }
	else moveDirection.y -= gravity * Time.deltaTime;
	
	if(Physics.Raycast(transform.position, moveDirection, hit))
			if(hit.point != Vector3(0.0, 0.0, 0.0)  hit.distance <= 1.0)
				moveDirection = Vector3(0.0, moveDirection.y, 0.0);
	
	transform.position += moveDirection;
	
	Physics.Raycast (transform.position, -Vector3.up, hit);
	
	grounded = (
		(hit.point != Vector3(0.0, 0.0, 0.0)) 
		(hit.distance < 1.5)  
		(moveDirection.y <= 0.0)
		);
	
    FPS();
}

function FPS()
{
    rx += Input.GetAxis("Mouse X") * rspeed;
    ry -= Input.GetAxis("Mouse Y") * rspeed;
    
	if(ry > 40.0) ry = 40.0;
	else if(ry < -40.0) ry = -40.0;
	
    CameraShake();
    
    transform.rotation = Quaternion.Euler(ry, rx, zrot);
}

function CameraShake()
{        
    if(grounded)
    {
        var vert = Input.GetAxis("Vertical");
        var hori = Input.GetAxis("Horizontal");
        
        moving = (vert != 0 || hori != 0);
        
        if(hori != 0)
        {
            if(hori > 0)
                if(zrot-- <= -10.0)
                    zrot = -10.0;
                
            if(hori< 0)
                if(zrot++ >= 10.0)
                    zrot = 10.0;
                    
            return;
        }
        
        if(vert > 0)
        {
            if(!sway)
            {
                if(zrot++ >= 7.5)
                    sway = true;
            }
            else
            {
                if(zrot-- <= -7.5)
                    sway = false;
            }
        }
    }
    else moving = false;
    
    if(!moving)
    {
        if(zrot < 0)
            zrot++;
        else if(zrot > 0)
            zrot--;
    }
}

Accounts for all collision to my knowledge. But, I’d still prefer if I could get the controller working without the camera jittering. Anyone?

var Response = "Damn! That's Javascript!";

function Start ()
{
        alert (Response);
}