- Home /
Relative camera movement at fixed height
I'm trying to script a player-controlled free-moving camera of the sort used in modern RTS games, where the camera is rotatable around the X and Y axis while the middle mouse button is held down, translatable along the X and Z axis while the right mouse button is held down, and translatable along the Y axis using the scroll wheel.
I have managed to get the rotation and Y axis translation working, but I don't know how to get the right mouse button click and drag effect to work. I'd like the camera to move laterally relative to the direction it is currently facing at a fixed height. Presently, it moves along the X axis in exactly the way that I would like due to Z rotation being fixed at 0, but gets closer or further from the ground along the Z axis, depending on the amount of X rotation.
Is there a simple way to make the camera move forward in the direction it is facing without affecting its global Y height?
Here's what isn't working for me:
//Adds Mouse X amount of relative force to X and Mouse Y amount of relative force to Z while the right button is held down
rightButton = Input.GetMouseButton(1);
if(rightButton == true){
xAmount = Input.GetAxis("Mouse X")*lateralSpeed;
zAmount = Input.GetAxis("Mouse Y")*lateralSpeed;
rigidbody.AddRelativeForce(-xAmount,0,-zAmount);
}
Would love to know where to go from here!
Answer by SeppahBaws · Sep 01, 2016 at 11:24 PM
Hi, this is what I managed to figure out for a fixed Y value:
void Update ()
{
SetFixedY(5.0f);
}
void SetFixedY (float n)
{
transform.position = new Vector3(transform.position.x, n, transform.position.z);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613122313im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
limit AddRelativeForce 1 Answer
AddRelativeForce to another object 1 Answer
Need help with moving camera 1 Answer
Camera Controls Need Help 1 Answer
Camera Follow problem D: 1 Answer