C# Restricting rotation about y and z axis while rotation around x axis
I have a problem regarding Quaternion.AngleAxis. My problem is similar to this one: https://answers.unity.com/questions/171286/restricting-rotation-to-the-y-axis-in-c.html I´m creating a laserbaem using a LineRenderer to point on a sourface, like in this video: https://www.youtube.com/watch?v=HaSABSofQbU
Now I want to move it with arrow keys. Moving the line in the Y direction causes no problems. (blue lines [picutre]) But when I try to move it in x direction at the bottom of the white plane it is not moving straight. Instead it moves like a curved line (red line).
Here is my script:` void moveLaser() { { // Rotate about X axis
if (Input.GetKey(KeyCode.DownArrow)) {
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
transform.Rotate(Vector3.right, Time.deltaTime * m_TurnSpeed, Space.Self);
}
if (Input.GetKey(KeyCode.UpArrow))
{
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
transform.Rotate(Vector3.left, Time.deltaTime * m_TurnSpeed, Space.Self);
}
// Rotate about Y axis
if (Input.GetKey(KeyCode.RightArrow))
{
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
transform.Rotate(Vector3.up, Time.deltaTime * m_TurnSpeed, Space.Self);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(eulerAngles.x, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
transform.Rotate(Vector3.down, Time.deltaTime * m_TurnSpeed, Space.Self);
}
}
}
}`
I also tried changing Space.Self to Space.World, but it didn´t solve the problem.
Thanks in advance!
[1]: /storage/temp/144289-lineproblem.png