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
3
Question by TOXIC_Chemist · Nov 22, 2012 at 12:46 AM · cameramovementwasd

Moving Camera with WASD

Hello, I've used C# XNA so im used to C#, but I need an example of what code for moving the camera would look like with WASD in Unity. Please include the keystate "gets" and how to access the camera's position variables, as well as how to set them. Thanks!

Comment
Add comment · Show 1
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 Johaneto · Apr 01, 2015 at 03:50 AM 0
Share

.... this does not work for Unity 5

6 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by CodeMasterMike · Nov 22, 2012 at 07:08 AM

Very simple example here:

     float xAxisValue = Input.GetAxis("Horizontal");
     float zAxisValue = Input.GetAxis("Vertical");
     if(Camera.current != null)
     {
         Camera.current.transform.Translate(new Vector3(xAxisValue, 0.0f, zAxisValue));
     }

So the "Input.GetAxis" variables are set in the Unity Input Manager (Edit->Project settings->Input) and in this case "Horizontal" means left and right, and "Vertical" forward and backward (z-axis). And this function returns a float value between '-Deadzone' (negative float) and 'Deadzone' (positive float).

If the button is not pressed at all, it returns 0 (zero). Remember that the GetAxis function interpolates the value, so it doesn't go straight to 0 (zero). If you want it to go straight to 0 (zero) when the key is released, you need to use "Input.GetAxisRaw" function.

So now when you have the translate value, you get your camera and you translate its transform with the values that you got. In my case above, I use the current camera, which is not so good if you have several different cameras in your project. But it works here as a example.

Little further reading:

GetAxis function documentation

GetAxisRaw function documentation

Input Manager documentation

Good luck!

Comment
Add comment · Show 1 · 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 beemer127 · Jul 12, 2014 at 11:34 PM 0
Share

This, of course, makes a lot of assumptions. It assumes that the camera is on a flat plane, and is not tilted downwards in a traditional 3rd person view, or RTS camera angle.

avatar image
1

Answer by MountDoomTeam · Nov 22, 2012 at 07:13 AM

keyboardorbit is a standard unity code in js and cs. look for keyboardOrbit in your project window search option, it should be there if you have the standard assets all installed.

this is it-

 //WASD to orbit, left Ctrl/Alt to zoom
 using UnityEngine;
 
 [AddComponentMenu("Camera-Control/Keyboard Orbit")]
 
 public class KeyboardOrbit : MonoBehaviour {
     public Transform target;
     public float distance = 20.0f;
     public float zoomSpd = 2.0f;
 
     public float xSpeed = 240.0f;
     public float ySpeed = 123.0f;
 
     public int yMinLimit = -723;
     public int yMaxLimit = 877;
 
     private float x = 22.0f;
     private float y = 33.0f;
 
     public void Start () {
        
         x = 22f;
         y = 33f;
 
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 
     public void LateUpdate () {
         if (target) {
             x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
             y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
             
             y = ClampAngle(y, yMinLimit, yMaxLimit);
             
         distance -= Input.GetAxis("Fire1") *zoomSpd* 0.02f;
             distance += Input.GetAxis("Fire2") *zoomSpd* 0.02f;
             
             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
             
             transform.rotation = rotation;
             transform.position = position;
         }
     }
 
     public static float ClampAngle (float angle, float min, float max) {
         if (angle < -360.0f)
             angle += 360.0f;
         if (angle > 360.0f)
             angle -= 360.0f;
         return Mathf.Clamp (angle, min, max);
     }
 }
 
 
 
 
Comment
Add comment · Show 1 · 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 12:03 PM 0
Share

That last answer gives 19 parsing errors in Unity! Anything better?

avatar image
1

Answer by CarterG81 · Mar 24, 2017 at 09:20 AM

For anyone who wants a QUICK Copy/Paste script just to move the Camera along the X/Z axis

 using UnityEngine;
 
 public class Debug_CameraMovement : MonoBehaviour
 {
     public int Speed = 50;
 
     void Update()
     {
         float xAxisValue = Input.GetAxis("Horizontal") * Speed;
         float zAxisValue = Input.GetAxis("Vertical") * Speed;
 
         transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y, transform.position.z + zAxisValue);
     }
 }

And if you want Q&E to zoom in/out Y-axis

 using UnityEngine;
 
 public class Debug_CameraMovement : MonoBehaviour
 {
     public int Speed = 50;

     void Update()
     {
         float xAxisValue = Input.GetAxis("Horizontal") * Speed;
         float zAxisValue = Input.GetAxis("Vertical") * Speed;
         float yValue = 0.0f;
 
         if (Input.GetKey(KeyCode.Q))
         {
             yValue = -Speed;
         }
         if (Input.GetKey(KeyCode.E))
         {
             yValue = Speed;
         }
 
         transform.position = new Vector3(transform.position.x + xAxisValue, transform.position.y + yValue, transform.position.z + zAxisValue);
     }
 }
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
-1

Answer by pseudonimus · Feb 08, 2017 at 08:20 PM

I have my version

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using RTS;
 using System;
 
 public class UserInput : MonoBehaviour {
     private Player player;
 
     // Use this for initialization
     void Start () {
         player = transform.root.GetComponent<Player>();
     }
     
     // Update is called once per frame
     void Update () {
 
 
         if (player.human)
         {
             MoveCamera();
             RotateCamera();
         }
 
 
 
 
     }
 
     private void RotateCamera()
     {
         Vector3 origin = Camera.main.transform.eulerAngles;
         Vector3 destination = origin;
 
         //detect rotation amount if ALT is being held and the Right mouse button is down
         if ((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && Input.GetMouseButton(1))
         {
             destination.x -= Input.GetAxis("Mouse Y") * ResourceManager.RotateAmount;
             destination.y += Input.GetAxis("Mouse X") * ResourceManager.RotateAmount;
         }
 
         //if a change in position is detected perform the necessary update
         if (destination != origin)
         {
             Camera.main.transform.eulerAngles = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.RotateSpeed);
         }
     }
 
     private void MoveCamera()
     {
         float xpos = Input.mousePosition.x;
         float ypos = Input.mousePosition.y;
         Vector3 movement = new Vector3(0, 0, 0);
 
         //Move the GameObject
         if (Input.GetKey("a"))
         {
             movement.x -= ResourceManager.ScrollSpeed;
         }
         if (Input.GetKey("s"))
         {
             movement.z -= ResourceManager.ScrollSpeed;
 
         }
         if (Input.GetKey("d"))
         {
             movement.x += ResourceManager.ScrollSpeed;
         }
         if (Input.GetKey("w"))
         {
 
             movement.z += ResourceManager.ScrollSpeed;
         }
 
         //horizontal camera movement
         if (xpos >= 0 && xpos < ResourceManager.ScrollWidth)
         {
             movement.x -= ResourceManager.ScrollSpeed;
         }
         else if (xpos <= Screen.width && xpos > Screen.width - ResourceManager.ScrollWidth)
         {
             movement.x += ResourceManager.ScrollSpeed;
         }
 
         //vertical camera movement
         if (ypos >= 0 && ypos < ResourceManager.ScrollWidth)
         {
             movement.z -= ResourceManager.ScrollSpeed;
         }
         else if (ypos <= Screen.height && ypos > Screen.height - ResourceManager.ScrollWidth)
         {
             movement.z += ResourceManager.ScrollSpeed;
         }
 
         movement = Camera.main.transform.TransformDirection(movement);
         movement.y = 0;
         //away from ground movement
         movement.y -= ResourceManager.ScrollSpeed * Input.GetAxis("Mouse ScrollWheel");
 
         //calculate desired camera position based on received input
         Vector3 origin = Camera.main.transform.position;
         Vector3 destination = origin;
         destination.x += movement.x;
         destination.y += movement.y;
         destination.z += movement.z;
 
         if (destination.y > ResourceManager.MaxCameraHeight)
         {
             destination.y = ResourceManager.MaxCameraHeight;
         }
         else if (destination.y < ResourceManager.MinCameraHeight)
         {
             destination.y = ResourceManager.MinCameraHeight;
         }
 
         //if a change in position is detected perform the necessary update
         if (destination != origin)
         {
             Camera.main.transform.position = Vector3.MoveTowards(origin, destination, Time.deltaTime * ResourceManager.ScrollSpeed);
         }
     }
 }
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 siman007 · Jan 31 at 07:08 PM

I needed a simple script to move the camera in a 3D scene using the keyboard or mouse for Windows/Mac users. It covers most of the basic WASD/Arrow/Mouse movements and has taken far more effort than I ever thought it would. Please feel free to use it as needed.

The code can be found on GitHub here

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

17 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

Related Questions

RTS Camera Rotation and Movement 0 Answers

Isometric WASD Movement using Rigidbodies 2 Answers

movement disable when camera changes view 1 Answer

Move camera forward/backwards with mouse wheel 1 Answer

Constraining a camera movement 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