Weird Movement

I have set up code to make a character move around in an fps-style fashion. This is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMovement : MonoBehaviour {
    public float sensitivityMultiplier = 1.5F;
    public float speed = 1F;
    public float xRotation = 5.0F;
    public float yRotation = 5.0F;
    // Use this for initialization
    void Start () {
	
}

// Update is called once per frame
void Update () {
    //This stuff is for rotating the camera
    yRotation += sensitivityMultiplier * Input.GetAxis("Mouse X");
    if (xRotation < 90 || Input.GetAxis("Mouse Y") >= 0)
    {
        if (xRotation > -90 || Input.GetAxis("Mouse Y") <= 0)
        {
            xRotation -= sensitivityMultiplier * Input.GetAxis("Mouse Y");
        }
    }
    transform.eulerAngles = new Vector3(xRotation, yRotation, 0);
    speed = 5.0F;
    if (Input.GetKey(KeyCode.W))
    {
        transform.Translate(Mathf.Sin(transform.localEulerAngles.y * Mathf.Deg2Rad) * speed * Time.deltaTime, 0, Mathf.Cos(transform.localEulerAngles.y * Mathf.Deg2Rad) * speed * Time.deltaTime);
    }
}

I’m pretty sure it was working before, but I think I did something that broke it. It seems like transform.localEulerAngles.y is double what it should be, but it’s not. Here’s an example. I start out with rotation values of (0,0,0). I rotate the character 90 degrees to the left (the y rotation is -90). When I hold W, it steadily decreases the Z position. The X position remains unaffected. This doesn’t make sense-- sin(-90) = -1. cos(-90) = 0. Shouldn’t it be decreasing the X position, not the Z? If I turn 180 degrees, I go backwards. Can anyone tell me what’s wrong? Note: Using transform.forward did not fix my problem either.

I think I have found the answer. In the transform.Translate(); function, there is an optional second parameter, relativeTo. Changing this to Space.World fixed my problem, as the function was using local axes instead of world axes. The modified code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMovement : MonoBehaviour {
public float sensitivityMultiplier = 1.5F;
public float speed = 1F;
public float xRotation = 5.0F;
public float yRotation = 5.0F;
// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
    yRotation += sensitivityMultiplier * Input.GetAxis("Mouse X");
    if (xRotation < 90 || Input.GetAxis("Mouse Y") >= 0)
    {
        if (xRotation > -90 || Input.GetAxis("Mouse Y") <= 0)
        {
            xRotation -= sensitivityMultiplier * Input.GetAxis("Mouse Y");
        }
    }
    transform.eulerAngles = new Vector3(xRotation, yRotation, 0);
    speed = 5.0F;
    if (Input.GetKey(KeyCode.W))
    {
        Vector3 translateTo = new Vector3(Mathf.Sin(transform.localEulerAngles.y * Mathf.Deg2Rad) * speed * Time.deltaTime, 0, Mathf.Cos(transform.localEulerAngles.y * Mathf.Deg2Rad) * speed * Time.deltaTime);
        transform.Translate(translateTo, Space.World);

    }
}

}