Hi guys! Right now I’m making my camera script, and I have encountered a problem.
I’m using this script right now to move the camera:
if (Input.GetKey (KeyCode.W)) {
Target.transform.Translate(Vector3.forward * MovementSpeed);
}
And it works fine. The only problem is that because the camera is tilted, it goes a bit down when I move the camera. How do I lock the translation on the Y axis so if the camera is tilted it’s still only moving on the z-axis on y = 10.
I hope this wasn’t to lousy written…
Thanks,
Niklas
if (Input.GetKey (KeyCode.W)) {
var v3 = Target.transform.forward;
v3.y = 0.0;
Target.transform.Translate(v3 * MovementSpeed * Time.deltaTime, Space.World);
}
If this code is executed in Update(), then you should introduce ‘Time.deltaTime’ to keep your code consistent across different frame rates. MovementSpeed will then need to increase and represents units per second.
The hackish way would be to do this:
if (Input.GetKey (KeyCode.W)) {
Target.transform.Translate(Vector3.forward * MovementSpeed);
Target.transform.position = (Target.transform.position.y, 10, Target.transform.position.z
}
But I’m sure their a better way by modifying the forward vector. Like this
Vector3 movingVector = Vector3.forward;
movingVector.y = 0;
That code is untested.
Easiest way to do that is to define a new variable like Vector3( Vector3.forward.x, 0, Vector3.forward.z);
That, however, will have limited uses, as you may want it to move in y occasionally. A little more context would help me help you better
for example, is this for a 3rd person camera?