- Home /
Rotation around Z axis wont lock correctly (2D)
I've recently started work on a small space game and im having trouble getting the camera to stop rotation around the Z axis, by this i mean that although the camera does lock it still moves a few degrees out of place, the images below explain it better than i do (i hope)
this image shows what i want the game to look like when the player (spaceship) is rotating, you can see that the edge of the map is perfectly straight
however in its current state when i rotate the player around the Z axis it seem to lock it in the rotation seen in the image below, it wont move any further than this but it still causes the camera to move by a few degrees. I'm very new to unity so i have no real idea why this is happening or how i can fix it, any help would be appreciated and thanks in advance
i only have two scripts in place at the moment, this issue started when i added the locking script (second one)
this script is attached to the player object
public float forwardSpeed = 10f;
public float reverseSpeed = 5f;
public float turnSpeed = 50f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.W))
transform.Translate(Vector2.up * forwardSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.S))
transform.Translate(-Vector2.up * reverseSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.A))
transform.Rotate(Vector3.forward, turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.D))
transform.Rotate(-Vector3.forward, turnSpeed * Time.deltaTime);
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Debug.Log (mouseWorldPosition);
}
this script is attached to the main camera, which is a child of the player
public bool PositionX = false;
public bool PositionY = false;
public bool PositionZ = false;
public bool RotationX = false;
public bool RotationY = false;
public bool RotationZ = false;
private Vector3 temp;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (PositionX == true) {
temp.x = 0.0f;
transform.position = temp;
}
if (PositionY == true) {
temp.y = 0.0f;
transform.position = temp;
}
if (PositionZ == true) {
temp.z = 0.0f;
transform.position = temp;
}
if (RotationX == true) {
transform.rotation = Quaternion.Euler(0.0f, transform.rotation.y, transform.rotation.z);
}
if (RotationY == true) {
transform.rotation = Quaternion.Euler(transform.rotation.x, 0.0f, transform.rotation.z);
}
if (RotationZ == true) {
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0.0f);
}
}
Answer by Pendrokar · May 06, 2014 at 02:05 PM
"this script is attached to the main camera, which is a child of the player"
Which means that when the player rotates so does the camera. If the camera isn't supposed to rotate with the player object, then the element that is rotating should be a child of the moving object and rotate that child instead.
Your answer
Follow this Question
Related Questions
really annoying rotation problem. 0 Answers
Rotating Produces Odd Results [Unity 2D] 1 Answer
How to account for situation using RotateAround 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers