Eulerangles and Transform.forward... something's off?

So, long story short I’m just trying to make a simple first-person-shooter for the moment (adding on stuff to actually make it interesting later on, but not worrying about that for now). I’m sure that a similar question must have been asked some time in the past but I honestly have no idea how to search for my problem. I’m using my mouse’s inputs to change the rotation on my camera, and attaching an object for movement. The object’s rotation is made to match the camera’s rotation, and then when WASD keys are pressed the object moves according to transform.forward and transform.right… however, when I rotate the camera to certain angles sideways, the movements get all screwed up.

I’m sorry if my explanation is bad, if you need to test yourself to tell the specifics of it, the only specifications for this scene is a camera and single object. Thankyou in advance for advance for anyone who can/does help out with this script. I’m sure it’s definitely not the most efficient, but I’m just trying to work with what I have.

script for the object

var movespeed:float = 4;
var zecamera:GameObject;

function Start () {

}

function FixedUpdate () 
{
	transform.rotation.y = zecamera.transform.rotation.y;
	if(Input.GetKey(KeyCode.W))
	{
		transform.position += transform.forward * movespeed * Time.deltaTime;
	}
	if(Input.GetKey(KeyCode.D))
	{
		transform.position += transform.right * movespeed * Time.deltaTime;
	}
	if(Input.GetKey(KeyCode.A))
	{
		transform.position += transform.right * -movespeed * Time.deltaTime;
	}
	if(Input.GetKey(KeyCode.S))
	{
		transform.position += transform.forward * -movespeed * Time.deltaTime;
	}
}

script for the camera

var playerlel:GameObject;
var sensitivityY:float = 1;
var sensitivityX:float = 1;
var movespeed:float = 4;

function Update ()
{
	//rotate up/down
	transform.eulerAngles.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
	//rotate left/right
	transform.eulerAngles.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
	transform.position = playerlel.transform.position;
}

You have a couple of related issues here. First from the script reference for Transform.eulerAngles:

Do not set one of the eulerAngles axis
separately (eg. eulerAngles.x = 10; )
since this will lead to drift and
undesired rotations.

The euler angles you set are not necessarily the ones you get back out. Run this bit of script:

transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);

The output is (0,180,180). So imagine you want to set (180, 45, 0), but you set it one axes at a time starting with ‘x’. You would get (0, 45, 180)…not even physically the rotation you wanted.

Given your code above, the solution is to maintain your own Vector3 and assign it. Never read the eulerAngles. Something like:

myRotation.y += Input.GetAxis("Mouse X") * sensitivityY * Time.deltaTime;
myRotation.x -= Input.GetAxis("Mouse Y") * sensitivityX * Time.deltaTime;
transform.eulerAngles = myRotation;