Question by
jordanoloughlin99 · Nov 12, 2020 at 07:08 PM ·
cameracamera-movementcamera rotate
Camera stuck at 0,0,0
Im trying to setup the camera in order to move it using the wasd keys along with middle mouse click and q or e for rotating. I have the camera as a child of a empty game object. However whenever I run the script my camera becomes stuck at position (0,0,0).
public class RTS_Cam : MonoBehaviour {
[Header("Camera")]
public Transform cam;
[Header("Movement Speeds")]
public float moveSpeed;
public float panSpeed;
[Header("Zoom")]
public float zoomSpeed;
[Header("Rotation")]
public float rotSpeed;
private Vector2 mouseLook;
private Vector3 startPos;
// Update is called once per frame
private void Start()
{
startPos = transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.H))
{
transform.position = startPos;
}
HandleMove();
HandleRot();
HandleZoom();
HandlePan();
}
private void HandlePan()
{
if (Input.GetMouseButton(1))
{
float mouseX = Input.GetAxis("Mouse X");
float mouseZ = Input.GetAxis("Mouse Y");
transform.position += transform.forward * -mouseZ * panSpeed * Time.deltaTime;
transform.position += transform.right * -mouseX * panSpeed * Time.deltaTime;
}
}
private void HandleMove()
{
float mouseX = Input.GetAxis("Horizontal");
float mouseZ = Input.GetAxis("Vertical");
transform.position += transform.forward * mouseZ * panSpeed * Time.deltaTime;
transform.position += transform.right * mouseX * panSpeed * Time.deltaTime;
}
private void HandleRot()
{
float horizontal = 0f;
if (Input.GetMouseButton(2))
{
horizontal = Input.GetAxis("Mouse X");
}
if (Input.GetKey(KeyCode.Q))
{
horizontal = -1f;
}
if (Input.GetKey(KeyCode.E))
{
horizontal = 1f;
}
Vector2 look = new Vector2(horizontal, 0f);
mouseLook += look * rotSpeed;
transform.localRotation = Quaternion.AngleAxis(mouseLook.x, transform.up);
}
private void HandleZoom()
{
float zoom = Input.GetAxis("Mouse ScrollWheel") * zoomSpeed;
cam.position = cam.forward * zoom;
}
}
camera-1.png
(50.0 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613015638im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help With Camera rotation on X and Y Axis (Target Focused) 1 Answer
Making the camera rotation in line with the ball 1 Answer
viewing on the Mouse Y axis not working. 0 Answers
,Third Person Movement Script Help 0 Answers
Using DampSmooth in EulerAngle is affecting the Position.Z value of the gameObject. 0 Answers