Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Neil Smith · Oct 04, 2013 at 04:50 PM · cameracamera movementviewcontroller

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 04, 2013 at 05:24 PM 1
Share

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));
 }


avatar image ALEXMARIAS · Jul 15, 2015 at 10:51 AM 0
Share

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 )

8 Replies

· Add your reply
  • Sort: 
avatar image
11

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));
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dris · Feb 13, 2015 at 05:49 AM 0
Share

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

avatar image fredzh · Feb 13, 2015 at 03:00 PM 0
Share

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.

avatar image timoveldt · Feb 13, 2015 at 04:02 PM 0
Share

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.

avatar image
2

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 04, 2013 at 05:35 PM 0
Share

@cowasockytommy - You need '+=' to make your code work.

avatar image cowasockytommy · Oct 07, 2013 at 12:37 AM 0
Share

;) off top of my head forgot about that hehe

avatar image OptimisticCode · Jul 22, 2014 at 06:39 PM 0
Share
  • on the Right Arrow and Up Arrow and a += on the Left Arrow and Down Arrow to be more specific.

avatar image Cherrychan22 · Jul 28, 2014 at 11:33 AM 0
Share

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.

avatar image
1

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Woody3d7 · Oct 24, 2016 at 11:56 AM 0
Share

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'!

avatar image CraftyMaelyss Woody3d7 · Mar 30, 2017 at 01:05 AM 0
Share

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.

avatar image
0

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);
         }
     }
 }
 
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Make a camera to Follow two players 2 Answers

Cinemachine screen shake not returning to original position 1 Answer

FPS cam for Roll -a-Ball like game 1 Answer

how to make the camera follow the player only in one axis (the z axis) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges