- Home /
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 is a really basic question. I strongly encourage you to work through some tutorials.
http://answers.unity3d.com/questions/12321/how-can-i-start-learning-unity-fast-list-of-tutori.html
The problem can be solved in just a couple of lines of code, but understanding those lines so you can use the concepts will take some effort:
var speed = 1.5;
function Update () {
transform.Translate(Vector3(-Input.GetAxis("Horizontal") * speed * Time.deltaTime, -Input.GetAxis("Vertical") * speed * Time.deltaTime, 0.0));
}
HI
The script above moves the camera relative to the camera orientation, if is rotated at x degree angle on y axis, moving it on the x will move it on both x and y
How can I move the camera position x, regardless of the orientation of the camera? ( regardless of the degrees of rotation on x,y,z )
Answer by fredzh · Aug 19, 2014 at 08:13 PM
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));
}
}
Thanks @fredzh,
This is what im looking for. One question, how about if there was a character to be moved with the keys, how do I incorporate it with the camera?
Thanks
depends on what you try to achieve.
It would be very inefficient to have the player object and the camera listen to the same key and move with the example script above.
If your goal is to have the camera move with the player you would use a camera following script attached to the camera and give it a followed object reference to the player while the arrow keys move the player. The following script would check in each update cycle if the followed object e.g. player has moved and move itself accordingly. google "smooth camera following script unity" you will find lots of examples and tutorials.
if your goal is to move the player in space independently from the camera you would need to separate the input used. e.g. in first person shooters you usually run around using the keyboard while ai$$anonymous$$g (moving the camera direction) with the mouse at the same time.
If I'm not mistaken, this script makes your character go twice as fast when holding both left and up. For a player character it (usually) strange to move faster when going at an angle then when going straight ahead. That's some refinement you should figure out for yourself though since you have to fine tune the gameplay.
Answer by cowasockytommy · Oct 04, 2013 at 04:57 PM
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.
on the Right Arrow and Up Arrow and a += on the Left Arrow and Down Arrow to be more specific.
Is there a way to make it so that there is a limit to how far someone can press the camera up/down/left/right? So that it's not endless. Sorry if that doesn't make sense. I mean so that the camera does not go on forever. I am trying to make a game for a child and I want to allow them to move the camera little so they feel more involved but I don't want them pressing it endlessly.
Also is there a way to make it so the camera has more a center point and looks up from that point ins$$anonymous$$d of moving up. I want to give an illusion that someone is looking up when really it is just a camera.
Thanks in advance.
Answer by Johaneto · Jul 15, 2015 at 02:24 PM
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);
}
Answer by Woody3d7 · Oct 24, 2016 at 11:42 AM
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!
Also PLEASE HELP, I can never post a question here (for years) because 'topics' always says 'no matches found' (I try words like Camera or Unity) What the heck am I supposed to type in the Topics box to get my questions posted? PLEASE HELP, TERRIBLY FRUSTRATED WITH THIS APPARENT ANTI-SUPPORT SITE!
I POST THIS QUESTION HERE BECAUSE I CAN'T FIGURE OUT WHAT TO TYPE IN 'TOPICS'!
If an error shows up in your console, just copy and paste that error into the search box. Or you can highlight the description and do a Google search on it. That's the best way to search I found. Also typing all in caps doesn't help either.
Answer by Tokars · Mar 08, 2017 at 02:53 PM
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);
}
}
}
Your answer
Follow this Question
Related Questions
How to stop the player rotation after i stop the camera? 1 Answer
ROBLOX Like Camera Script 2 Answers
Smooth camera on moving platform 1 Answer
GUI.Box and Camera Movement 1 Answer