- Home /
RTS Camera movement wrong after rotation
Hello, i am trying to move a point(parent of the camera) into the direction the camera is looking using following code:
if (Input.GetKey(KeyCode.W))
{
Vector3 forwardVec = new Vector3(Camera.main.transform.forward.x, 0, transform.forward.z);
transform.position += forwardVec * Time.deltaTime * movementSpeed;
camMoved = true;
}
I am using the following code to rotate the camera:
if (Input.GetMouseButton(1))
{
float h = horizontalMouseSpeed * Input.GetAxis("Mouse X");
float v = verticalMouseSpeed * Input.GetAxis("Mouse Y");
transform.LookAt(transform);
Camera.main.transform.Translate((Vector3.left * h) * Time.deltaTime);
Camera.main.transform.Translate((Vector3.down * v) * Time.deltaTime);
if (Camera.main.transform.position.y < transform.position.y)
{
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x,
transform.position.y, Camera.main.transform.position.z);
}
}
The point moves into the wrong direction after rotating the camera a bit. The script is component of the point the camera is rotating around.
Answer by Crusader_NN · Jul 10, 2019 at 11:43 PM
I made a mistake by assuming thy Y in the forward vector was redundant if i didn't want to change it's height. I used the full Camera.Main.transform.forward now and changed the Y position of the object to 0 afterwards.
Your answer
Follow this Question
Related Questions
2D Aiming in a 3D game 0 Answers
Setting value of transform.rotation in code, C# 2 Answers
Flip over an object (smooth transition) 3 Answers
How to make Camera position Independent of its Rotation? 1 Answer