- Home /
Input + Camera Movement
I wrote a script to move the camera via the keyboard. I pieced this together using various piece of the Roll A Ball tutorial. However, the camera continues moving for about a half second after I release the key. Obviously that's not optimal performance. Here's the script:
public class camera_controller : MonoBehaviour
{
void Update ()
{
float horizontal = Input.GetAxis ("Pan Horizontal");
float vertical = Input.GetAxis ("Pan Vertical");
float zoom = Input.GetAxis ("Zoom");
Vector3 position = transform.position;
if (horizontal < 0) position.x += .1f;
if (horizontal > 0) position.x -= .1f;
if (vertical < 0) position.z += .1f;
if (vertical > 0) position.z -= .1f;
if (zoom < 0) position.y += .5f;
if (zoom > 0) position.y -= .5f;
transform.position = position;
}
}
There seems to be a LOT of variations on how to do this, but most of the examples I've seen are using Time.deltaTime in the math somewhere. I didn't originally have that, but adding it in hasn't really affected anything, aside from slowing the scrolling way down.
if (horizontal < 0) position.x += 10f * Time.deltaTime;
What about the way I wrote my script is causing the input delay? I'd rather understand the code rather than just copy/paste something that "works" without having learned anything.
Answer by ShadyProductions · Apr 27, 2017 at 10:46 AM
You could do something much easier:
void Update ()
{
float horizontal = Input.GetAxis ("Pan Horizontal");
float vertical = Input.GetAxis ("Pan Vertical");
float zoom = Input.GetAxis ("Zoom");
float speed = 5f;
transform.Translate(horizontal * speed * Time.deltaTime, zoom * speed * Time.deltaTime, vertical * speed * Time.deltaTime);
}
Also, shouldn't zoom be the Z axis instead of the Y and vertical be the Y axis and not the Z?
@ShadyProductions Unity does not like the transform.translate line, is there another way to do this?
Your answer
![](https://koobas.hobune.stream/wayback/20220612120618im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enable/Disable button input 1 Answer
simulate pc keyboard input in android touch screen keyboard 0 Answers
query keyboard mapping in new input system 0 Answers
Keyboard Input + Camera Movement 1 Answer
Scroll input carrying over to next scene 0 Answers