3D Platformer Turn and Strafe(Java Script)

I am working on a 3D platformer and I want the player to be able to move throughout the game play area as well as strafe back and forth while facing a direction and shooting. The problem is that the player will sometimes not face completely forward while strafing and shooting, sometimes the player will do this at an angle.

The controls I have working now work like this, A and D move you forwards and backwards and W and S move closer and further from the camera. I have my code working sort of but , whenever the player turns around and then tries to strafe shoot, the player will begin shooting at an angle instead of straight on.

If you tap the A or the D key in the direction you are facing the player will correct itself but I want to have to avoid having to do this. Here is the code(Java Script) that I am working with. It’s a collection of code from here that I have found on the net. So please let me know if I am doing something wrong.

var moveSpeed 	: float = 6.0;
var jumpSpeed 	: float = 8.0;
var gravity 	: float = 20.0;
var runSpeed 	: float = 1.0;
var strafeSpeed : float = 6.0;

var startPosition : Vector3; //For storing the starting position of the player. 



private  var dead = false;
static var moveDirection   : Vector3 = Vector3.zero;
static  var strafeDirection : Vector3 = Vector3.zero;


function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if (hit.gameObject.tag == "worldKill")
	{
		dead = true;
		HealthController.LIVES -= 1;
		//This is a Debug to make sure health is working.
		//HealthController.HEALTH -= 50;
	}	
} 

function Start()
{
	//Get the starting position of the player so that we can respawn them there if they die. 
	startPosition = transform.position;
}

function Update()
{
	var controller : CharacterController = GetComponent(CharacterController);
	if(controller.isGrounded)
	{
		
		strafeDirection = Vector3(Input.GetAxis("Horizontal"),0,0);
		
		moveDirection = Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal"));
		moveDirection *= moveSpeed;
		
		//This needs much more testing to fully understand 
		if (strafeDirection.sqrMagnitude > 0.01)
		{
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection), 1);
			
		}

		if(Input.GetButton("Jump"))
		{
			moveDirection.y = jumpSpeed;
		}
	
	}
	
	// Apply Gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	controller.Move(moveDirection * Time.deltaTime);
}

function LateUpdate()
{
	if(dead)
	{
		//Move the player back to the spawn
		transform.position = startPosition;
		gameObject.Find("Main Camera").transform.position = Vector3(0,0,0);
		dead = false;
		
	}
}

Thanks for the help!!!

Your problem occurs when you press a horizontal movement key at the same time at the same time as a vertical movement key. The issue is where you do the Slerp() on line 47. You are passing moveDiretion to the LookRotation() when you should be passing strafeDiretion().

Note for line 45 I would:

 if (strafeDirection != Vector3.zero)

You just want to avoid passing a zero vector to Quaternion.LookRotation().

I tried this and I get the movement that I want but now when the player goes to strafe they no longer face forward and fire but fire exactly 90 degrees from where I want them to fire. Its almost like I need the inverse of their movement. Here is what I am working with now. The variable that you suggested that I add is called test in my code and is set to an initial value of 0.01.

function Update()
{
	var controller : CharacterController = GetComponent(CharacterController);
	if(controller.isGrounded)
	{

		//This handles all of the forward and side to side movment.
		moveDirection = Vector3(Input.GetAxis("Vertical"),0,Input.GetAxis("Horizontal"));
		//This gives us the option to change the speed. 
		moveDirection *= moveSpeed;
		
		/*-- This is what is used to do the straffing but this the code that I am having the most issuses with.
		  -- I am not sure what to do here to get the player to be able to strafe side to side with still looking
		  -- or back wards and maintaing some fluidness to their movment through out the play area.  			
		*/
		//strafeDirection = Vector3(0,0,Input.GetAxis("Horizontal"));
		strafeDirection = Vector3(Input.GetAxis("Vertical") * test,0,Input.GetAxis("Horizontal"));
		
		//This is where we set the direction that the player is facing but it faces the player incorrectly
		//if (strafeDirection.sqrMagnitude > 0.01)
		// Make sure that this is set to moveDirection
		if (moveDirection != Vector3.zero)
		{
			// This sets the direction of the player and should be set to strafeDirection 
			transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(strafeDirection), 1);
		}

		if(Input.GetButton("Jump"))
		{
			moveDirection.y = jumpSpeed;
		}
	
	}

I think I am pretty close here. Probably just some little thing that I am overlooking. If I do figure this out I will post back. Also should I change the movement to use specific keys? Or is using Vertical and Horizontal an okay method to use instead?