How to move a camera only using the arrow keys?

I’m new to Unity and want to simply move a camera around a static scene using the arrows keys on your keyboard. How do I go about this?

this works:

void Update()
{
	if(Input.GetKey(KeyCode.RightArrow))
	{
		transform.Translate(new Vector3(speed * Time.deltaTime,0,0));
	}
	if(Input.GetKey(KeyCode.LeftArrow))
	{
		transform.Translate(new Vector3(-speed * Time.deltaTime,0,0));
	}
	if(Input.GetKey(KeyCode.DownArrow))
	{
		transform.Translate(new Vector3(0,-speed * Time.deltaTime,0));
	}
	if(Input.GetKey(KeyCode.UpArrow))
	{
		transform.Translate(new Vector3(0,speed * Time.deltaTime,0));
	}
}

create a c# script and put this in it.

public float speed = 5.0f;
void Update()
{
    if(Input.GetKey(KeyCode.RightArrow))
	{
		transform.position = new Vector3(speed * Time.deltaTime,0,0);
	}
	if(Input.GetKey(KeyCode.LeftArrow))
	{
		transform.position = new Vector3(speed * Time.deltaTime,0,0);
	}
	if(Input.GetKey(KeyCode.DownArrow))
	{
		transform.position = new Vector3(0,speed * Time.deltaTime,0);
	}
	if(Input.GetKey(KeyCode.UpArrow))
	{
		transform.position = new Vector3(0,speed * Time.deltaTime,0);
	}
}

then put this script on your camera
notice (0,0,0) this is x,y,z put the line with speed in it where ever axis your wanting to change

need javascript? let me know.

hi, it’s been a while since I post this, and since I casually came back here, here I would like to post the final code that totally worked:

#pragma strict

var walkSpeed = 150;
var rotateSpeed = 100;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update () {
var controller : CharacterController = GetComponent.<CharacterController>();

if (controller.isGrounded) {
    // We are grounded, so recalculate
    // move direction directly from axes
    moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    moveDirection = transform.TransformDirection(moveDirection);
    moveDirection *= walkSpeed * Time.deltaTime;

    //also with jump ;)
    if (Input.GetButton ("Jump")) {
        moveDirection.y = jumpSpeed;
    }
}

// Apply gravity 
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
controller.Move(moveDirection * Time.deltaTime);

// rotate controller
var horizontalDir = parseFloat(Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime);
transform.Rotate(0, horizontalDir, 0);
}

Can someone tell me why my keyboard cam script won’t work? Unity v5.2

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

// Use this for initialization
void Start () {

}

// Update is called once per frame
public float speed = 5.0f;
void Update()
{
    if (Input.GetKey(KeyCode.RightArrow))
    {
        transform.position = new Vector3(speed * Time.deltaTime, 0, 0);
    }
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        transform.position = new Vector3(speed * Time.deltaTime, 0, 0);
    }
    if (Input.GetKey(KeyCode.DownArrow))
    {
        transform.position = new Vector3(0, speed * Time.deltaTime, 0);
    }
    if (Input.GetKey(KeyCode.UpArrow))
    {
        transform.position = new Vector3(0, speed * Time.deltaTime, 0);
    }
}  

This script was posted here, but doesn’t seem to work! 2 days just trying to move a camera! Please help!

Hang this script on GameObject which you want to move

using UnityEngine;
using System.Collections;

public class MoveClass : MonoBehaviour
{
	public float speed = 0.1f;
	public void FixedUpdate()
	{
		if(Input.GetKey(KeyCode.RightArrow))
		{
			transform.position = new Vector3(transform.position.x + speed, transform.position.y, transform.position.z);
		}
		if(Input.GetKey(KeyCode.LeftArrow))
		{
			transform.position = new Vector3(transform.position.x - speed, transform.position.y, transform.position.z);
		}
		if(Input.GetKey(KeyCode.DownArrow))
		{
			transform.position = new Vector3(transform.position.x, transform.position.y - speed, transform.position.z);
		}
		if(Input.GetKey(KeyCode.UpArrow))
		{
			transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
		}
	}
}

Another way using Input.GetAxis():

You can set axes in Edit → Project Settings → Input Manager

You may want to change using Vector3 axis z instead of y, depending on your configuration.

public class CameraScript : MonoBehaviour
{
    public float moveSpeed = 5f;

    void Update()
    {
        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");
        var step = moveSpeed * Time.deltaTime;

        transform.Translate(new Vector3(horizontal * step, vertical * step, 0));
    }
}

Select the “Main Camera” object, click “Add Component”, “New Script”. Call the new Script “FlyCamera”. Paste this script in there after you open/edit the script in visual studio: Unity Script to give camera WASD + mouse control · GitHub

Save. Start the game. You can now fly around using WASD and the mouse.

Here is how I solved for a 2d game:
float currentXThrow = 0f;
float currentYThrow = 0f;
float defaultZ;
[SerializeField] float moveSpeed = 1f;

private void Start()
{
    defaultZ = transform.position.z;
}

// Update is called once per frame
void Update()
{
    currentXThrow = Input.GetAxisRaw("Horizontal");
    currentYThrow = Input.GetAxisRaw("Vertical");
}

private void FixedUpdate()
{
    var xMove = currentXThrow * moveSpeed;
    var yMove = currentYThrow * moveSpeed;
    var myPos = transform.position;

    transform.position = new Vector3(myPos.x + xMove, myPos.y + yMove, defaultZ);
}