The scenario is as follows - user can click on a character to make the main camera look at that selected character, using this code:
RaycastHit hit;
var cameraCenter = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, Camera.main.nearClipPlane));
if (Physics.Raycast(cameraCenter, transform.forward, out hit, 1000)) {
var obj = hit.transform.gameObject;
if (obj == _activeCharacter) {
return;
}
}
var relativePos = _activeCharacter.transform.position - transform.position;
var targetRotation = Quaternion.LookRotation(relativePos.normalized);
var rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
transform.rotation = rotation;
}```
I also want to enable camera movement using WSAD (or arrows) keys, so I created this code:
```private void MoveCamera() {
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 20;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 20;
if (x == 0 && z == 0) {
return;
}
transform.Translate(x, 0, z, Space.Self);
}```
You may see the problem already - if the camera is rotated on the X axis, then pressing the W key will actually make camera zoom on whatever it's pointing at. What I would like to achieve is rather to move the camera along the forward vector that's pointing forward, laying flat on a virtual plane at the height the camera is at the moment.
Maybe an ugly drawing will help you understand what I'm after:
![](https://i.stack.imgur.com/yVfOu.png)